How will I access the current version of the app so that I can at least show in Text Widget?
3 Answers
You can set the app version as an environment variable in your build config like so:
flutter build apk --dart-define=APPVERSION=1.0.0
then use it in the code like so:
const String APPVERSION =
String.fromEnvironment('APPVERSION', defaultValue: 'dev-build');
You can then use this in your text widget like so:
Text(APPVERSION),
Comments
Try out package_info package:-
PackageInfo packageInfo = await PackageInfo.fromPlatform();
if (Platform.isAndroid) {
var androidAppVersion = packageInfo.version;
log("Android Version---->$androidAppVersion");
log("Build Number---->${packageInfo.buildNumber}");
} else {
var iosAppVersion = packageInfo.version;
log("Ios Version---->$iosAppVersion");
}
Comments
Here is my working solution addressing directly the version inside the pubspec.yaml:
class AppVersionWidget extends StatefulWidget {
const AppVersionWidget({super.key});
@override
TabAboutInformationState createState() => TabAboutInformationState();
}
class AppVersionWidgetState extends State<AppVersionWidget> {
late String appVersion = '?';
@override
void initState() {
super.initState();
getAppVersion();
}
Future<void> getAppVersion() async {
PackageInfo packageInfo = await PackageInfo.fromPlatform();
setState(() {
appVersion = packageInfo.version;
});
}
@override
Widget build(BuildContext context) {
return Text(appVersion);
}
}