问题
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
- Add this to your package's pubspec.yaml file:
dependencies:
package_info: ^0.4.0+3
- Import the file into your dart file:
import 'package:package_info/package_info.dart';
- 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