How to get build and version number of Flutter app

2020-02-24 12:50发布

I am currently developing an application which is currently in beta mode. Due to this, I would like to show them what version they are on. For example, "v1.0b10 - iOS". So far, I have got this code: Text("Build: V1.0b10 - " + (Platform.isIOS ? "iOS" : "Android")). How would I be able to get the build version and number within flutter?

2条回答
淡お忘
2楼-- · 2020-02-24 13:24

You can use package_info.

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: ^0.4.0+3
  1. Import the file into your dart file:
import 'package:package_info/package_info.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 away/async

PackageInfo.fromPlatform().then((PackageInfo packageInfo) {
  String appName = packageInfo.appName;
  String packageName = packageInfo.packageName;
  String version = packageInfo.version;
  String buildNumber = packageInfo.buildNumber;
});
查看更多
▲ chillily
3楼-- · 2020-02-24 13:34

There is a plugin that will help you get the version name and number.

Add the dependency

In pubspec.yaml add the package_info package.

dependencies:
  package_info: ^0.4.0+13

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/package_info.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 versionName = packageInfo.version;
String versionCode = packageInfo.buildNumber;

See also

查看更多
登录 后发表回答