Flutter - Get App Version & Build Number Programmatically

This tutorial shows you how to programmatically get the version and build number of a Flutter application.

Just like most other frameworks, Flutter supports application versioning. It's quite common in your application code to get the version and build number. For example, if you want to force users that still use older versions to update the application, or just for displaying the information to the users If you want to get those values, it can be done with the help of a package. Read the example below.

Dependencies

The package_info_plus is a library that allows you to get information like version and build number in an easy way. To use the package, you need to add it in the pubspec.yaml file and run flutter pub get.

  dependencies:
    package_info_plus: ^3.0.3

Get App Version and Build Number

In Flutter, you can define the app version and build number by changing the value of version property in the pubspec.yaml file. The version name contains numbers separated with two dots (e.g. 1.0.2). A version can have multiple build numbers. The build number is an optional value defined after the version name, separated with + symbol. For example, 1.0.2+5 means the version is 1.0.2, while the build number is 5.

With the package_info_plus package, getting the app version and build number programmatically becomes very easy. First of all, you need to add the following import at the top of the file

  import 'package:package_info_plus/package_info_plus.dart';

Then, you need to get an instance of PackageInfo by calling PackageInfo.fromPlatform().

  final PackageInfo packageInfo = await PackageInfo.fromPlatform();

If you need to get the version and build number, you can obtain the values from the version and buildNumber properties respectively.

  packageInfo.version
  packageInfo.buildNumber

Besides those two properties, the PackageInfo class of package_info_plus package has some property as follows:

  • version: The package version. versionName on Android, CFBundleShortVersionString on iOS.
  • buildNumber: The build number. versionCode on Android, CFBundleVersion on iOS.
  • buildSignature: The build signature. signing key signature (hex) on Android, empty string on iOS.
  • appName: The application name. application/label on Android, CFBundleDisplayName on iOS.
  • packageName: The package name. getPackageName on Android, bundleIdentifier on iOS.
  • installerStore: The installer store.

Full Code

  import 'package:flutter/material.dart';
  import 'package:package_info_plus/package_info_plus.dart';
  
  void main() => runApp(const MyApp());
  
  class MyApp extends StatelessWidget {
  
    const MyApp({Key? key}) : super(key: key);
  
    @override
    Widget build(BuildContext context) {
      return const MaterialApp(
        title: 'Woolha.com Flutter Tutorial',
        home: GetAppVersionExample(),
      );
    }
  }
  
  class GetAppVersionExample extends StatefulWidget {
  
    const GetAppVersionExample({Key? key}) : super(key: key);
  
    @override
    State<StatefulWidget> createState() {
      return _GetAppVersionExampleState();
    }
  
  }
  
  class _GetAppVersionExampleState extends State<GetAppVersionExample> {
  
    String? _version;
    String? _buildNumber;
    String? _buildSignature;
    String? _appName;
    String? _packageName;
    String? _installerStore;
  
  
    @override
    void initState() {
      super.initState();
      _getAppVersion();
    }
  
    void _getAppVersion() async {
      final PackageInfo packageInfo = await PackageInfo.fromPlatform();
  
      final version = packageInfo.version;
      final buildNumber = packageInfo.buildNumber;
      final buildSignature = packageInfo.buildSignature;
      final appName = packageInfo.appName;
      final packageName = packageInfo.packageName;
      final installerStore = packageInfo.installerStore;
  
      setState(() {
        _version = version;
        _buildNumber = buildNumber;
        _buildSignature = buildSignature;
        _appName = appName;
        _packageName = packageName;
        _installerStore = installerStore;
      });
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: const Text('Woolha.com Flutter Tutorial'),
          backgroundColor: Colors.teal,
        ),
        body: SizedBox(
          width: double.infinity,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text('Version : ${_version ?? '-'}'),
              Text('Build number : ${_buildNumber ?? '-'}'),
              Text('Build signature : ${_buildSignature ?? '-'}'),
              Text('App name : ${_appName ?? '-'}'),
              Text('Package name : ${_packageName ?? '-'}'),
              Text('Installer store : ${_installerStore ?? '-'}'),
            ],
          ),
        ),
      );
    }
  }

Summary

Getting the version and build number of a Flutter application can be done using the package_info_plus package.

You can also read about: