Prompt Update App Dialog In Flutter Application

Almost every application has update feature. There might be other methods to do it but I will share my experience that how I did it. May be it will help you and if you have any feedback or better option please let me know.

Basically our app on start will compare current version installed with new version information stored on Firebase remote config settings and if its greater, then it will prompt users to update application.

Image for post
Image for post

Packages We Need:

We need following packages:

package_info will help us to find current installed version of application.

url_launcher will help us to trigger app store / play store url from our app.

firebase_remote_config will help us to store latest version on the server

Imports

import 'dart:io';import 'package:url_launcher/url_launcher.dart';import 'package:package_info/package_info.dart';import 'package:firebase_remote_config/firebase_remote_config.dart';

In our main.dart

@override
void initState() {
try {
versionCheck(context);
} catch (e) {
print(e);
}
super.initState();
}

We have to define our app URL links for play store and app store. You can save it on firebase remote or locally on your app. I am doing it in my app:

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 method

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');
}
}

lets trigger dialog

_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(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(PLAY_STORE_URL),
),
FlatButton(
child: Text(btnLabelCancel),
onPressed: () => Navigator.pop(context),
),
],
);
},
);
}

and our _launchURL method:

_launchURL(String url) async {
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}

Thats it we are done with coding part now in your Firebase under remote config add a new parameter:

force_update_current_version

with a value for example

force_update_current_version :1.0.0

Now whenever you release a new version just update parameter in remote config and press publish changes button at top and user will be promoted to update version next time he would open our app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store