If you need to get the version code or the package name, use this package_info in your pubspec.yaml
package_info: 0.4.1
take the current version from https://pub.dev/packages/package_info
if you neeed to use it before starting the whole application set this in you main.dart and call it from the begining of the initState,
let's suppose you've got a mechanism to have control over your version code, and each time you deliver a new app version code you want your users are forced to update the app, so one of many solutions to do it would be doing and enpoint in your api which get you the current application version code which is going to be compared with the is taken from the installed mobile phone, if they math the app goes on, if they do not, then you force the user to be redirected to a view in which the user is forced to update the new app
void getVersionCode() async {
PackageInfo packageInfo = await PackageInfo.fromPlatform();
String versionName = packageInfo.version;
String versionCode = packageInfo.buildNumber;
String packageName = packageInfo.packageName;
Response res = await get(urlServer + "/v1/applicationversion",
headers: {'Content-Type': 'application/json;charset=utf-8'});
final jsonResponse = jsonDecode(res.body);
if (versionCode == jsonResponse.toString()) {
if (mounted){
setState(() {
packageName = packageName;
checkingApplicationVersion = true;
});
}
}
}
@overridevoid initState() {super.initState();getVersionCode();}@overrideWidget build(BuildContext context) {return WillPopScope(onWillPop: () {return new Future(() => false);},child: checkingApplicationVersion? getStandardView(): ForcingUpdatingApp(packageName));// child: Text("dada"));}
in that way before just before getStandardView() is rendered there is a boolean which is analyzed if it is true, it will render the standard the normal view, if it is false it will be redirected to force the user to update to the new version
now to redirect the user from the app to the pay store, you need to set up in the pubspec.yaml store_redirect in this the version is store_redirect: ^1.0.1
be sure about taking the stable one
now to use it from code, just use
StoreRedirect.redirect(
androidAppId: "com.example.yourpackagename",
iOSAppId: "585027354");
and that's it, so you can trigger it from a button ike this
RaisedButton(
onPressed: () {
StoreRedirect.redirect(
androidAppId: packageName,
iOSAppId: "585027354");
},
color: Colors.lightGreen[700],
child: const Text('ACTUALIZAR',
style: TextStyle(fontSize: 17, color: Colors.white)),
)
Comments
Post a Comment