Skip to content

Instantly share code, notes, and snippets.

@naumanahmed19
Last active February 25, 2024 14:51
Show Gist options
  • Save naumanahmed19/edf6380eee944e5afd36b87b7a755548 to your computer and use it in GitHub Desktop.
Save naumanahmed19/edf6380eee944e5afd36b87b7a755548 to your computer and use it in GitHub Desktop.
Flutter Force Update IOS and Android App Version
//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';
}
}
@tmldoc
Copy link

tmldoc commented Dec 31, 2020

@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")

@TheGlorySaint
Copy link

@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