How to get build and version number of Flutter Web app

3,528

Solution 1

As a temporary workaround you can create a separate file with the version info in it:

web_version_info.dart

class WebVersionInfo {
  static const String name = '1.0.0';
  static const int build = 1;
}

You can use that for all platforms or in your code you can use kIsWeb to just use it for the web:

Future<String> _getAppVersion() async {
  if (kIsWeb) {
    return WebVersionInfo.name;
  } else {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    return packageInfo.version;
  }
}

Of course, this is not a great solution because now you need to remember to update the version and build information in both pubspec.yaml and in WebVersionInfo every time you update the app.

Solution 2

If you use beta channel you can use package_info_plus plugin that appears to be a drop-in-replacement for package_info. So all you need to change is pubspec.yaml and your import. (I only use version so there could be differences that I haven't noticed)

Change pubspec and your import

pubspec.yaml: package_info_plus: '>=0.6.3 <2.0.0'

import: import 'package:package_info_plus/package_info_plus.dart'

Reference:

Github issue 46609

Solution 3

For those using Linux and in order to improve Suragch's answer, I suggest automating the build process using bash scripts. For that, we need two scripts: one to increase the version build number and another to call the flutter build command itself, forwarding the parameters. That way, if you prefer to just increment the version build number manually, you can just call the update script and then 'flutter build' later, but if you want to do everything in one step, you can call the builder script.

You will only need to edit the '.app_version' file as the version changes.

The '.build_seq', '.version_number' files are always rewritten, and the '.app_version' file is created only if it is not found.

The scripts:

updversion.sh:

#!/bin/bash

if [ -f ".app_version" ]; then
    VER=`cat .app_version`
else
    VER="1.0.0"
    echo $VER > .app_version
fi

if [ -f ".build_seq" ]; then
    BLD=`cat .build_seq`
else
    BLD='0'
fi

((BLD++))
echo $BLD > .build_seq
echo "Ver: $VER ($BLD)" > .current_version

echo "
// Auto-generated by updversion.sh. Do not edit.

class WebVersionInfo {
  static const String name = '$VER';
  static const int build = $BLD;
}

" > lib/version_info.dart

exit 0

buildweb.sh:

#!/bin/bash

./updversion.sh

flutter build web $*

exit $?
Share:
3,528
Suragch
Author by

Suragch

Read my story here: Programming was my god

Updated on December 18, 2022

Comments

  • Suragch
    Suragch over 1 year

    The keyword here is Web. I can get the build and version number of a Flutter app by using the package_info plugin, but if it is running on the web I can't. How do I get the package info for a Flutter Web app?

    I'll include an answer below as a temporary fix, but I am looking a solution that gets the version info from pubspec.yaml.

  • L--
    L-- over 3 years
    I agree with comments in the github issue that it is strange that they have both package_info_plus and package_info. Hopefully they release web support also in package_info soon so this whole question is answered by flutter upgrade and update your package_info plugin.