Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get build and version number of Flutter app

People also ask

How do I check my android version on Flutter app?

You can try new_version plugin. Using this plugin you can get installed App Version, Playstore App Version and app url which can redirect to playstore. Show activity on this post. For using it from command line or CLI, you need a pure Dart code.

How do I find my Flutter package name?

xml file under the android>app> src>debug and android>app> src>profile. Step 2: Open the android>app> build. gradle file. Find the defaultConfig section and update the applicationId to the new package name.


You can use package_info_plus.

The versions are extracted from:

Android:

build.gradle, versionCode and versionName

iOS:

Info.plist, CFBundleVersion

Usage

Add the dependency

  1. Add this to your package's pubspec.yaml file:
dependencies:
  package_info_plus: ^1.0.6
  1. Import the file into your dart file:
import 'package:package_info_plus/package_info_plus.dart';
  1. if your method is marked as async:
PackageInfo packageInfo = await PackageInfo.fromPlatform();

String appName = packageInfo.appName;
String packageName = packageInfo.packageName;
String version = packageInfo.version;
String buildNumber = packageInfo.buildNumber;

If you don't want to use await/async:

PackageInfo.fromPlatform().then((PackageInfo packageInfo) {
  String appName = packageInfo.appName;
  String packageName = packageInfo.packageName;
  String version = packageInfo.version;
  String buildNumber = packageInfo.buildNumber;
});

Note: This answer has been updated to reflect the fact that the package_info plugin is deprecated and redirects to package_info_plus.

Version name and build number

At development time, you can easily find the version name and build number of a Flutter or Dart project by inspecting pubspec.yaml. Here is an example:

version: 1.1.0+2

This is case the version name is 1.1.0 and the build number is 2.

However, if you want to get these values at runtime, you should use a plugin.

Add the dependency

In pubspec.yaml add the package_info_plus package.

dependencies:
  package_info_plus: ^1.0.2

Update the version number to the current one.

Import the package

In the file that you need it, add the following import.

import 'package:package_info_plus/package_info_plus.dart';

Get the version name and code

In your code you can get the app version name and code like this:

PackageInfo packageInfo = await PackageInfo.fromPlatform();
String version = packageInfo.version;
String code = packageInfo.buildNumber;

See also

  • How to set build and version number of Flutter app
  • How to get build and version number of Flutter Web app

After package_info installation, you can use it directly with future builder in your widget tree:

 FutureBuilder<PackageInfo>(
              future: PackageInfo.fromPlatform(),
              builder: (context, snapshot) {
                switch (snapshot.connectionState) {
                  case ConnectionState.done:
                    return Align(
                      alignment: Alignment.bottomCenter,
                      child: Text(
                        'Version: ${snapshot.data!.version}',),
                      ),
                    );
                  default:
                    return const SizedBox();
                }
              },
            ),

RE the multiple references to package_info, please note that this package has been deprecated and the recommended replacement is the Flutter Community Plus Plugins version, package_info_plus.


You can use the get_version to query information about the application Version Name, Version Code, Platform and OS Version, and App ID on iOS and Android

Add this to your package's pubspec.yaml file:

dependencies:
  get_version: ^0.2.2

Now in your Dart code, you can use:

import 'package:get_version/get_version.dart';

Go to build.gradle and update:

defaultConfig {
  versionCode 1
  versionName "1.0"
  minSdkVersion 16
  testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

How to Use

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  String _projectVersion = '';
  String _projectCode = '';
  String _projectAppID = '';
  String _projectName = '';

  @override
  initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      platformVersion = await GetVersion.platformVersion;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    String projectVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      projectVersion = await GetVersion.projectVersion;
    } on PlatformException {
      projectVersion = 'Failed to get project version.';
    }

    String projectCode;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      projectCode = await GetVersion.projectCode;
    } on PlatformException {
      projectCode = 'Failed to get build number.';
    }

    String projectAppID;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      projectAppID = await GetVersion.appID;
    } on PlatformException {
      projectAppID = 'Failed to get app ID.';
    }

    String projectName;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      projectName = await GetVersion.appName;
    } on PlatformException {
      projectName = 'Failed to get app name.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
      _projectVersion = projectVersion;
      _projectCode = projectCode;
      _projectAppID = projectAppID;
      _projectName = projectName;
    });
  }
  
}

enter image description here