How to get build and version number of Flutter app

拜拜、爱过 提交于 2019-12-03 20:37:22

问题


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?


回答1:


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



回答2:


This is a more detailed answer for future visitors.

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+3

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

  • How to set build and version number of Flutter app


来源:https://stackoverflow.com/questions/53672171/how-to-get-build-and-version-number-of-flutter-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!