Flutter automatically update application when new version is available

狂风中的少年 提交于 2021-02-17 19:14:37

问题


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.


回答1:


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.

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.

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.




回答2:


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 s2

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.




回答3:


You can use https://pub.dev/packages/flutter_downloader and put the user to download the update (apk file) directly from your app




回答4:


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.




回答5:


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


来源:https://stackoverflow.com/questions/58811133/flutter-automatically-update-application-when-new-version-is-available

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!