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?
- 5,317
- 8
- 34
- 74
-
1see this link: https://blog.maskys.com/how-to-get-the-version-build-number/ – M Karimi Jan 02 '21 at 09:57
7 Answers
You can use package_info_plus.
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_plus: ^1.0.6
- Import the file into your dart file:
import 'package:package_info_plus/package_info_plus.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 await/async:
PackageInfo.fromPlatform().then((PackageInfo packageInfo) {
String appName = packageInfo.appName;
String packageName = packageInfo.packageName;
String version = packageInfo.version;
String buildNumber = packageInfo.buildNumber;
});
- 263
- 2
- 18
- 79,114
- 19
- 208
- 182
-
In IOS this give version 1.0.0 instead of 1.0.0+6. Can you guide me what could be the problem ? – Wajid khan Jul 01 '19 at 15:21
-
1@Wajidkhan, 1.0.0 is the version number and 6 is the build number. See [this](https://stackoverflow.com/questions/54357468/how-to-set-build-and-version-number-of-flutter-app). – Suragch Jan 09 '20 at 00:13
-
1does this means I need to manually modify/maintain different versions for iOS and Android? is there a unified place? – Alan Steiman May 30 '20 at 12:44
-
3Unhandled Exception: MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/package_info) only on release apk – Jithin Joy Aug 19 '20 at 16:01
-
2If you want your app working on Web, Android and IOS use "package_info_plus" instead. https://pub.dev/packages/package_info_plus. – Alex Correia Nov 22 '20 at 13:50
-
use the new version from https://pub.dev/packages/package_info/install – Shamis Shukoor Mar 22 '21 at 09:51
Note: This answer has been updated to reflect the fact that the package_info plugin is deprecated and redirects to package_info_plus.
Version name and build number
At development time, you can easily find the version name and build number of a Flutter or Dart project by inspecting pubspec.yaml. Here is an example:
version: 1.1.0+2
This is case the version name is 1.1.0 and the build number is 2.
However, if you want to get these values at runtime, you should use a plugin.
Add the dependency
In pubspec.yaml add the package_info_plus package.
dependencies:
package_info_plus: ^1.0.2
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_plus/package_info_plus.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 version = packageInfo.version;
String code = packageInfo.buildNumber;
See also
- 428,106
- 278
- 1,284
- 1,317
-
-
9@Volleyball, at the time I answered the question, the accepted answer was much shorter (check the edit history). Later that user added a lot of content so they are basically the same now. – Suragch Sep 10 '19 at 13:54
-
1Sorry I didn't see that, you are right, your answer should be the accepted one, +1 from me. – Sep 10 '19 at 16:48
-
`package_info_plus` - nice. Thanks so much for sharing. Like it more than `package_info` as it supports more platforms. – Björn W Jul 30 '21 at 08:06
-
More about the deprecated packages plugins here: https://github.com/flutter/plugins/blob/master/README.md#deprecated – Son Nguyen Aug 15 '21 at 10:10
install package_info_plus, then you can use it directly with future builder in your widget tree:
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();
}
},
),
- 11,211
- 8
- 60
- 67
RE the multiple references to package_info, please note that this package has been deprecated and the recommended replacement is the Flutter Community Plus Plugins version, package_info_plus.
- 99
- 1
- 8
You can use the get_version to query information about the application Version Name, Version Code, Platform and OS Version, and App ID on iOS and Android
Add this to your package's pubspec.yaml file:
dependencies:
get_version: ^0.2.2
Now in your Dart code, you can use:
import 'package:get_version/get_version.dart';
Go to build.gradle and update:
defaultConfig {
versionCode 1
versionName "1.0"
minSdkVersion 16
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
How to Use
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;
});
}
}
- 37,512
- 17
- 201
- 182
You can try new_version plugin. Using this plugin you can get installed App Version, Playstore App Version and app url which can redirect to playstore.
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);
}
});
}
}
Custom Alert Dialog Box
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);
}
),
],
)
);
- 770
- 7
- 14
For using it from command line or CLI, you need a pure Dart code.
I used the following script:
// 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]);
}
You can run it from the project root folder:
dart run scripts/get_version_name.dart
- 14,849
- 11
- 103
- 128