habatot haba
habatot haba

Reputation: 499

Flutter automatically update application when new version is available

How can I implement auto-updating of my application when a new version is released without using Google Play Store? I'm using JSON to check the version.

Upvotes: 49

Views: 100209

Answers (8)

Mahesh Jamdade
Mahesh Jamdade

Reputation: 20221

This is the top most requested feature since the early days of Flutter repository and it is still open as of today. This has not been implemented into the flutter tooling and the framework due to security reasons, you can read more about the technicalities in this issue #14430 thread.

The good news is the creator of the issue is a former Flutter team member and they came up with a solution https://shorebird.dev/ to tackle this problem. I have used that in one of my apps https://github.com/maheshmnj/vocabhub and it works seamlessly for both IOS and Android and can be integrated into an existing app within minutes.

  1. Install the Shorbird CLI tool
  2. Initialize your project with shorebird
  3. Create a release using shorebird
  4. Release your app to stores
  5. Publish the patch using Shorebird
  6. Updates are automatically downloaded in the background when the user opens the app, this is also configurable to check for updates or notify when the update is available.

You can see a quick demo here https://www.youtube.com/watch?v=7KDgFvdogsE

Shorebird has a comprehensive and well-written doc to explains all the steps in detail https://docs.shorebird.dev/

One thing to note is that this is a paid service, However, on a free tier, you a get a generous 5k patches/month (at the time of writing) for free. So definitely checkout their pricing.

Upvotes: 0

Eric Su
Eric Su

Reputation: 1165

This method gives you more control of your user's update flow instead of relying on third party UI solutions.

First, install the upgrader package and use their API to get the current version on the App Store or Play Store. Then, compare it to the currently installed app version on the user's device. You can use the package_info_plus package to do that. To compare the version names directly, use the the version package. Finally, you can decide what you want to do if the store version is greater than the installed version. For example, you can show an alert dialog, snackbar, or notification message to take the user to the app store, or you can implement your own UI.

How to use the upgrader package to get the store version number:

(Change the country code if your app is not available in the US)

import 'dart:developer';
import 'dart:io';
import 'package:html/dom.dart';
import 'package:upgrader/upgrader.dart';

Future<String?> getStoreVersion(String myAppBundleId) async {
  String? storeVersion;
  if (Platform.isAndroid) {
    PlayStoreSearchAPI playStoreSearchAPI = PlayStoreSearchAPI();
    Document? result = await playStoreSearchAPI.lookupById(myAppBundleId, country: 'US');
    if (result != null) storeVersion = playStoreSearchAPI.version(result);
    log('PlayStore version: $storeVersion}');
  } else if (Platform.isIOS) {
    ITunesSearchAPI iTunesSearchAPI = ITunesSearchAPI();
    Map<dynamic, dynamic>? result = 
                   await iTunesSearchAPI.lookupByBundleId(myAppBundleId, country: 'US');
    if (result != null) storeVersion = iTunesSearchAPI.version(result);
    log('AppStore version: $storeVersion}');
  } else {
    storeVersion = null;
  }
  return storeVersion;
}

Upvotes: 4

Mohamed Amin
Mohamed Amin

Reputation: 1067

Important Note:

Update notification will not appear if you installed your app through emulator(AVD) because the App signing key certificate of the tested app is different of that the google play use.

So,make sure to install your app through google play itself(not through android studio),and by this way,the update notification will work and the signed app certificate is the same as that google play provide to your app.

Upvotes: 2

Rasel Khan
Rasel Khan

Reputation: 4239

You can use this package
https://pub.dev/packages/upgrader

Wrap your whole widget in UpgradeAlert widget like this ->

Scaffold(
          appBar: AppBar(title: Text('Upgrader Example')),
          body: UpgradeAlert(
            child: Center(child: Text('Your widget is here')),
          )),

if an update available
On ios style On android style

Upvotes: 1

awaik
awaik

Reputation: 12287

==== Update December 2021

new nice package, recommend this one https://pub.dev/packages/new_version

==== Actually, in June 2020, we have more possibilities with Flutter. Among them:

1. Make updates inside the app. Display two kinds of notifications if the app has the new version.

enter image description here

Plugin - https://pub.dev/packages/in_app_update (works only on Android, iOS doesn't support such functionality)

2. When a newer app version is available in the app store, a simple alert prompt widget or card is displayed.

enter image description here

Works Android & iOS. Plugin - https://pub.dev/packages/upgrader

3. Use Firebase in-app messaging. It gives flexibility in the messages and forms of notifications.

  • But a lot more boilerplate code and work.
  • But you can use it for other messages and notifications when your app is growing.

https://firebase.google.com/docs/in-app-messaging

4. Make it by yourself. Maybe even less code then in the case with Firebase messaging.

Upvotes: 45

Phani Rithvij
Phani Rithvij

Reputation: 4477

It's not possible without using the google play store if you want an automatic update.

  • You need to handle the versioning yourself by hosting the apks somewhere (say github for example) and check if a new version exists and prompt the user whether they want to update the app.
  • Then download the new apk in the background. (You can use this package)
  • Then open the apk when it's done downloading using one of the methods mentioned here
  • Which will then prompt the user whether to allow installing apks from your app

A plugin exists if you want to do it using the play store. in_app_update Which wraps the android in-app update functionality s 2

Their official example on github

If you also want an iOS solution, then it's not possible. You could redirect the user to the AppStore. Some more info on the distribution methods available for apple apps.

There is this method which might work if you have an enterprise license.

Whereas if you have a server running, have an endpoint to query the latest versions and another endpoint that allows users to download the apk.

Use something like github releases if you don't have a server.

Upvotes: 14

Bruno Camargos
Bruno Camargos

Reputation: 327

Maybe this help you

Backend backend = Backend.instance();
PackageInfo packageInfo = await PackageInfo.fromPlatform();
String packageName = packageInfo.packageName;

Response r;
try {

  r = await backend.io.get("https://play.google.com/store/apps/details?id=$packageName&hl=en");
  if(r.statusCode == 200){
    String data = r.data;

    String pat1 = 'Current Version</div><span class="htlgb"><div class="IQ1z0d"><span class="htlgb">';
    String pat2 = '</span>';

    int p1 = data.indexOf(pat1) + pat1.length;
    String f = data.substring(p1, data.length);
    int p2 = f.indexOf(pat2);

    String currentVersion = f.substring(0, p2);

    return currentVersion;
  }

  return null;

} catch (e) {

  errors = backend.interceptor.errors;

  return null;
}

Upvotes: 3

Abel Mekonnen
Abel Mekonnen

Reputation: 1905

My way of implementation is to use a variable and compare that variable from a database. It could be Firebase or any other. For this example, I will use Firebase realtime database.

int version = 1;
void checkLatestVersion(){
    
//Here i am getting just the value of the latest version stored on firebase.    
databaseReference.child("version").child("latestRealase").once().then((snapshot){
      if(snapshot.value != null){
          int versionNumberFromDatabase = int.parse(snapshot.value));
          if(versionNumberFromDatabase>version){
             print("the app needs to be updated");
             //HERE you can create a dialog to display and force users to update
          }else{
             print("The app doesn't to be need updated ");
          }
      }
     });
 }

The above example will work for both Android and iOS, but there is a package that will let you update your app from the app itself. Check the documentation.

in_app_update 1.1.7

This will enables In-App Updates on Android using the official Android APIs.

Upvotes: 0

Related Questions