如何获取Flutter应用的内部版本和版本号


77

我当前正在开发一个处于beta模式的应用程序。因此,我想向他们展示它们的版本。例如,“ v1.0b10-iOS”。到目前为止,我已经收到以下代码:Text("Build: V1.0b10 - " + (Platform.isIOS ? "iOS" : "Android"))。我如何才能在Flutter中获得内部版本和编号?


Answers:


140

您可以使用package_info

这些版本摘自:

Android:

build.gradle, versionCode and versionName

iOS:

Info.plist, CFBundleVersion

用法

添加依赖项

  1. 将此添加到包的pubspec.yaml文件中:
dependencies:
  package_info: ^0.4.0+16
  1. 将文件导入到dart文件中:
import 'package:package_info/package_info.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;
});

在IOS中,版本为1.0.0,而不是1.0.0 + 6。您能指导我可能是什么问题吗?
Wajid khan

@ Wajidkhan,1.0.0是版本号,6是内部版本号。看到这个
苏拉奇

这是否意味着我需要为iOS和Android手动修改/维护不同的版本?有一个统一的地方吗?
艾伦·斯蒂曼

2
未处理的异常:MissingPluginException(在通道plugins.flutter.io/package_info上未找到方法getAll的实现)仅在版本apk上发布
Jithin Joy,

如果您希望您的应用程序在Web上运行,则Android和IOS使用“ package_info_plus”来代替。pub.dev/packages/package_info_plus
亚历克斯·科雷亚

66

有一个插件可以帮助您获取版本名称和版本号。

添加依赖项

在pubspec.yaml中添加package_info包。

dependencies:
  package_info: ^0.4.0+13

将版本号更新为当前版本号。

导入包裹

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

import 'package:package_info/package_info.dart';

获取版本名称和代码

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

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

也可以看看


我看不出这与接受的答案有何不同?

4
@排球,在我回答问题时,接受的答案要短得多(请检查编辑历史记录)。后来,该用户添加了很多内容,因此现在它们基本相同。
Suragch

1
抱歉,我没有看到,您是正确的,您的答案应该是被接受的答案,我的+1。

0

您可以使用get_version查询有关应用程序版本名称,版本代码,平台和操作系统版本以及iOS和Android上的应用程序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;
    });
  }
  
}

在此处输入图片说明


0

您可以尝试new_version插件。使用此插件,您可以安装App Version,Playstore App Version和可以重定向到Playstore的应用URL。

新版本插件

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);
            }
        ),
      ],
    )
);
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.