Complete Guide to Retrieving Build and Version Numbers in Flutter Apps

Nov 24, 2025 · Programming · 13 views · 7.8

Keywords: Flutter | Version Management | package_info_plus

Abstract: This article provides a comprehensive guide on dynamically retrieving build numbers and version names in Flutter applications. Using the package_info_plus plugin, developers can easily access version information defined in Android's build.gradle and iOS's Info.plist. The guide includes complete dependency setup, code examples, and both asynchronous and synchronous implementation approaches to help accurately display app version information during beta testing.

Introduction

In mobile app development, particularly during beta testing phases, accurately displaying application version information is crucial for test feedback and issue tracking. The Flutter framework provides convenient methods to retrieve this information, and this article delves into how to use the package_info_plus plugin to achieve this functionality.

Basic Concepts of Version Information

In mobile app development, version information is typically divided into version name and build number. The version name serves as a user-visible identifier, such as "1.0.0", while the build number is used for internal version management, usually as an incrementing number. In Flutter projects, this information can be configured uniformly in the pubspec.yaml file and then mapped to the native configuration files of Android and iOS respectively.

Configuring Dependencies

To use the package_info_plus plugin, you first need to add the dependency in your project's pubspec.yaml file. Open the file and add the following under the dependencies section:

dependencies:
  package_info_plus: ^1.0.6

After adding, run the flutter pub get command to download and install the plugin. Ensure you use the latest version number, which can be checked on pub.dev.

Importing the Package

In the Dart file where you need to use version information, import the package_info_plus package:

import 'package:package_info_plus/package_info_plus.dart';

This step ensures that the classes and methods provided by the plugin are accessible in your code.

Retrieving Version Information

The package_info_plus plugin provides the PackageInfo.fromPlatform() method to retrieve version information. This method returns a PackageInfo object containing properties such as app name, package name, version name, and build number. Depending on the code structure, you can call it asynchronously or synchronously.

Asynchronous Approach

If the calling method is within an asynchronous function, you can use the await keyword:

PackageInfo packageInfo = await PackageInfo.fromPlatform();

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

This approach is suitable for most modern Flutter applications as it avoids blocking the main thread.

Synchronous Approach

If you prefer not to use asynchronous operations, you can handle the returned Future using the then method:

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

This method might be more straightforward in some simple scenarios, but care should be taken to avoid delays during UI construction.

Practical Application Example

Combining with the original query's requirements, here is a complete example demonstrating how to display version information and platform identification in a Flutter app:

import 'package:flutter/material.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'dart:io';

class VersionDisplay extends StatefulWidget {
  @override
  _VersionDisplayState createState() => _VersionDisplayState();
}

class _VersionDisplayState extends State<VersionDisplay> {
  String version = '';
  String buildNumber = '';

  @override
  void initState() {
    super.initState();
    _loadVersionInfo();
  }

  Future<void> _loadVersionInfo() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    setState(() {
      version = packageInfo.version;
      buildNumber = packageInfo.buildNumber;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Text('Build: V$version$buildNumber - ${Platform.isIOS ? "iOS" : "Android"}');
  }
}

In this example, we create a stateful Widget that asynchronously loads version information during initialization and dynamically displays it in the UI. Note that we use the Platform class from dart:io to detect the running platform.

Platform-Specific Configuration

The package_info_plus plugin reads version information from each platform's configuration files:

Ensure that version information is correctly set in these files so that the plugin can read it accurately.

Important Considerations

When using package_info_plus, keep the following points in mind:

Conclusion

With the package_info_plus plugin, Flutter developers can easily retrieve and utilize application version information, which is essential for app testing, release, and user support. The complete examples and detailed explanations provided in this article should help you quickly implement this functionality.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.