Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get build and version number of Flutter Web app

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.

like image 310
Suragch Avatar asked Mar 12 '20 03:03

Suragch


3 Answers

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.

like image 69
Suragch Avatar answered Sep 22 '22 14:09

Suragch


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

like image 27
L-- Avatar answered Sep 22 '22 14:09

L--


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 $?
like image 39
Warlei Alves Avatar answered Sep 23 '22 14:09

Warlei Alves