如何获取 Flutter 应用程序的构建和版本号

How to get build and version number of Flutter app

我目前正在开发一个目前处于测试模式的应用程序。因此,我想向他们展示他们使用的是什么版本。例如,"v1.0b10 - iOS"。到目前为止,我得到了这个代码:Text("Build: V1.0b10 - " + (Platform.isIOS ? "iOS" : "Android"))。我如何才能在 flutter 中获取构建版本和编号?

您可以使用 package_info_plus.

版本提取自:

Android:

build.gradle, versionCode and versionName

iOS:

Info.plist, CFBundleVersion

用法

添加依赖

  1. 将此添加到您的包的 pubspec.yaml 文件中:
dependencies:
  package_info_plus: ^1.0.6
  1. 将文件导入您的 dart 文件:
import 'package:package_info_plus/package_info_plus.dart';
  1. 如果您的方法被标记为 async:
PackageInfo packageInfo = await PackageInfo.fromPlatform();

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

如果你不想使用await/async:

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

注意:此答案已更新以反映 package_info plugin is deprecated and redirects to package_info_plus.

版本名称和内部版本号

在开发时,您可以通过检查pubspec.yaml轻松找到Flutter或Dart项目的版本名称和构建号。这是一个例子:

version: 1.1.0+2

这是版本名称为 1.1.0 且内部版本号为 2.

的情况

但是,如果您想在运行时获取这些值,则应使用插件。

添加依赖

pubspec.yaml中添加package_info_plus包。

dependencies:
  package_info_plus: ^1.0.2

将版本号更新为current

导入包

在您需要的文件中,添加以下导入。

import 'package:package_info_plus/package_info_plus.dart';

获取版本名称和代码

在您的代码中,您可以获得应用程序版本名称和代码,如下所示:

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

另见

您可以在iOS和Android上使用get_version查询有关应用程序版本名称、版本代码、平台和OS版本以及App ID的信息

将此添加到您的包的 pubspec.yaml 文件中:

dependencies:
  get_version: ^0.2.2

现在在您的 Dart 代码中,您可以使用:

import 'package:get_version/get_version.dart';

转到 build.gradle 并更新:

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

如何使用

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;
    });
  }
  
}

您可以尝试 new_version 插件。使用此插件,您可以获得安装的应用程序版本、Playstore 应用程序版本和可以重定向到 playstore 的应用程序 url。

New Version Plugin

void versionCheck() async {
    final NewVersion newVersion = NewVersion(context: context);
    VersionStatus versionStatus = await newVersion.getVersionStatus();
    if (versionStatus != null && versionStatus.canUpdate) {
      await ConfirmDialog(
          context: context,
          title: 'Update Available',
          body: Text('A new version, ${versionStatus.storeVersion}, is available.'),
          acceptButton: 'Update Now',
          cancelButton: 'Update Later'
      ).then((ConfirmAction res) async {
        if (res == ConfirmAction.CONFIRM && await canLaunch(versionStatus.appStoreLink)) {
          await launch(versionStatus.appStoreLink, forceWebView: false);
        }
      });
    }
  }

自定义警报对话框

enum ConfirmAction{ CONFIRM, CANCEL }
Future<ConfirmAction> ConfirmDialog({
  BuildContext context,
  String title,
  Widget body,
  String acceptButton,
  String cancelButton
})
=> showDialog(
    context: context,
    barrierDismissible: false,
    builder: (BuildContext context) => AlertDialog(
      title: Wrap(
        crossAxisAlignment: WrapCrossAlignment.center,
        spacing: 4,
        children: <Widget>[
          Text(title)
        ],
      ),
      content: Wrap(
        runSpacing: 10,
        children: <Widget>[
          body,
        ],
      ),
      actions: <Widget>[
        FlatButton(
            padding: EdgeInsets.all(6),
            child: Text(acceptButton ?? 'Confirm'),
            onPressed: (){
              Navigator.of(context, rootNavigator: true).pop(ConfirmAction.CONFIRM);
            }
        ),
        FlatButton(
            padding: EdgeInsets.all(6),
            child: Text(cancelButton ?? 'Cancel'),
            onPressed: (){
              Navigator.of(context, rootNavigator: true).pop(ConfirmAction.CANCEL);
            }
        ),
      ],
    )
);

安装package_info_plus,然后您可以在您的小部件树中直接将其与未来的构建器一起使用:

 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 对 package_info 的多次引用,请注意此包已被弃用,推荐的替代品是 Flutter Community Plus Plugins version, package_info_plus.

要从命令行或 CLI 使用它,您需要纯 Dart 代码。

我使用了以下脚本:

// ignore_for_file: avoid_print
import 'dart:io';

import 'package:path/path.dart';
import 'package:yaml/yaml.dart';

String pathToYaml = join(dirname(Platform.script.toFilePath()), '../pubspec.yaml');

Future<YamlMap> loadPubspec() async => loadYaml(await File(pathToYaml).readAsString());

void main() async {
  var pubspec = await loadPubspec();
  print(pubspec['version'].toString().split('+')[0]);
}

您可以从项目根文件夹中运行它:

dart run scripts/get_version_name.dart