flutterdartversioningsemantic-versioning

Flutter - How to get the build number of the app?


I understand the usage of package_info_plus package and I am able to get the version number correctly.

The issue here is that the Build number of our application is decided by Github Runner on release as follows:

--build-number=${{github.run_number}}

I want to display this number in-app. Is it possible?

    return FutureBuilder(
        future: PackageInfo.fromPlatform(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            return Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                design.BodyText(context.localized.appVersion),
                design.BodyText(
                  "${snapshot.data!.version}(${snapshot.data!.buildNumber})",
                ),
              ],
            );
          }
          return const SizedBox();
        })

The build number here is from pubspec.yaml. I would want the Github runner based build number to be shown.


Solution

  • Assuming you're using Github Actions to build and deploy your app, you could edit your yml file to put the build number in your env variables. For example :

    name: PRODUCTION
    
    env:
      APP_VERSION: ${{github.ref_name}}
      APP_BUILD_NUM : ${{github.run_number}}
    
    on:
      push:
         ...
    

    And then from your flutter app, you could get it with :

    String appBuildNum =
    const String.fromEnvironment('APP_BUILD_NUM', defaultValue: '123');