Last active
February 25, 2024 14:51
-
-
Save naumanahmed19/edf6380eee944e5afd36b87b7a755548 to your computer and use it in GitHub Desktop.
Flutter Force Update IOS and Android App Version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Prompt users to update app if there is a new version available | |
//Uses url_launcher package | |
import 'package:url_launcher/url_launcher.dart'; | |
const APP_STORE_URL = | |
'https://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftwareUpdate?id=YOUR-APP-ID&mt=8'; | |
const PLAY_STORE_URL = | |
'https://play.google.com/store/apps/details?id=YOUR-APP-ID'; | |
versionCheck(context) async { | |
//Get Current installed version of app | |
final PackageInfo info = await PackageInfo.fromPlatform(); | |
double currentVersion = double.parse(info.version.trim().replaceAll(".", "")); | |
//Get Latest version info from firebase config | |
final RemoteConfig remoteConfig = await RemoteConfig.instance; | |
try { | |
// Using default duration to force fetching from remote server. | |
await remoteConfig.fetch(expiration: const Duration(seconds: 0)); | |
await remoteConfig.activateFetched(); | |
remoteConfig.getString('force_update_current_version'); | |
double newVersion = double.parse(remoteConfig | |
.getString('force_update_current_version') | |
.trim() | |
.replaceAll(".", "")); | |
if (newVersion > currentVersion) { | |
_showVersionDialog(context); | |
} | |
} on FetchThrottledException catch (exception) { | |
// Fetch throttled. | |
print(exception); | |
} catch (exception) { | |
print('Unable to fetch remote config. Cached or default values will be ' | |
'used'); | |
} | |
} | |
//Show Dialog to force user to update | |
_showVersionDialog(context) async { | |
await showDialog<String>( | |
context: context, | |
barrierDismissible: false, | |
builder: (BuildContext context) { | |
String title = "New Update Available"; | |
String message = | |
"There is a newer version of app available please update it now."; | |
String btnLabel = "Update Now"; | |
String btnLabelCancel = "Later"; | |
return Platform.isIOS | |
? new CupertinoAlertDialog( | |
title: Text(title), | |
content: Text(message), | |
actions: <Widget>[ | |
FlatButton( | |
child: Text(btnLabel), | |
onPressed: () => _launchURL(Config.APP_STORE_URL), | |
), | |
FlatButton( | |
child: Text(btnLabelCancel), | |
onPressed: () => Navigator.pop(context), | |
), | |
], | |
) | |
: new AlertDialog( | |
title: Text(title), | |
content: Text(message), | |
actions: <Widget>[ | |
FlatButton( | |
child: Text(btnLabel), | |
onPressed: () => _launchURL(Config.PLAY_STORE_URL), | |
), | |
FlatButton( | |
child: Text(btnLabelCancel), | |
onPressed: () => Navigator.pop(context), | |
), | |
], | |
); | |
}, | |
); | |
} | |
_launchURL(String url) async { | |
if (await canLaunch(url)) { | |
await launch(url); | |
} else { | |
throw 'Could not launch $url'; | |
} | |
} |
@DJSL1NKY
instead of config.appstore url, copy your app store URL as a string "http://play.google.....etc"
then
onPressed function calls ==> _launchURL("http://play.google.....etc")
@tmldoc I have done for Android and iOS like this
FlatButton(
child: Text(btnLabel),
onPressed: () => _launchURL(
APP_STORE_URL),
),
And for the URL i created a String.
If i create a class called Config, add there all of the Firebase Remote Config and add then Config
infront of the URL is this working?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@naumanahmed19
Thanks for your Work. I do get 4 Errors.
I have the initState in my main.dart and the rest imported from another File.
The first two are at the initState. At versionCheck(context) i get at the context "Undefined name 'context'." and at the super.initState i get I"nvalid context for 'super' invocation".
At the leftover Code i get at the FlatButton for _launchURL(Config.APP_STORE_URL) and at the _launchURL(Config.PLAY_STORE_URL) the error at Config. At both i get following Error: "Undefined name 'Config'.
Try correcting the name to one that is defined, or defining the name."
I would be blessed if you can help me.
I wish you a healty start in the new Year.