1. Description
iOS and Android updates are completely different.
iOS can only jump to AppStore, which is easier to implement
Android needs to download the apk package. Since there are many Android models, we use dart to connect to third parties (here) native android download library.
The update interface and download update are processed separately.
iOS has no download progress, but Android has it.
2. Code
2.1 Just use url_launcher on iOS
if () { final url = "/cn/app/id1380512641"; // Just change the number after id to your own application id if (await canLaunch(url)) { await launch(url, forceSafariVC: false); } else { throw 'Could not launch $url'; } }
2.1 Android implementation
2.1.1 Add download library in android/app/ file
dependencies { // Only copy this line implementation ':app-updater:1.0.4-androidx' }
2.1.2 Add storage permissions in
<uses-permission android:name=".WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name=".READ_EXTERNAL_STORAGE"/>
2.1.3 Writing plugins in android project
package .flutter_yuedu.plugins; import ; import ; import ; import ; import ; import ; import ; import ; import ; /** UpdateVersionPlugin */ public class UpdateVersionPlugin implements { private static String TAG = "MY_UPDATE"; private static Context context; public UpdateVersionPlugin(Context context) { = context; } /** Plugin registration. */ public static void registerWith(Registrar registrar) { final EventChannel channel = new EventChannel((), "/update_version"); (new UpdateVersionPlugin(())); } @Override public void onListen(Object o, eventSink) { if (().length() < 5) { (TAG, "URL Error", o); return; } if (!().startsWith("http")){ (TAG, "URL Error", o); } AppUpdater update = new AppUpdater(context,()).setUpdateCallback(new UpdateCallback() { Map data = new HashMap<String, Object>(); // Send data to Flutter private void sendData() { (data); } @Override public void onDownloading(boolean isDownloading) { } @Override public void onStart(String url) { ("start", true); ("cancel", true); ("done", true); ("error", false); ("percent", 1); sendData(); } @Override public void onProgress(int progress, int total, boolean isChange) { int percent = (int)(progress * 1.0 / total * 100); if (isChange && percent > 0) { ("percent", percent); sendData(); } } @Override public void onFinish(File file) { ("done", true); sendData(); } @Override public void onError(Exception e) { ("error", ()); sendData(); } @Override public void onCancel() { ("cancel", true); sendData(); } }); (); } @Override public void onCancel(Object o) { (TAG, "Cancel Download - Integrated third-party downloads do not provide cancellation methods"); } }
2.1.4 Register the plugin in MainActivity
// Register update component in the onCreate method(registrarFor("/update_version"));
We need to get the download progress, so we use EventChannel to continuously communicate with one-way.
2.3 dart-side implementation
static const channelName = '/update_version'; static const stream = const EventChannel(channelName); // Progress subscription StreamSubscription downloadSubscription; int percent = 0; // Start downloading void _startDownload() { if (downloadSubscription == null) { downloadSubscription = stream .receiveBroadcastStream() .listen(_updateDownload); } } // Stop monitoring progress void _stopDownload() { if (downloadSubscription != null) { (); downloadSubscription = null; percent = 0; } } // Progress download void _updateDownload(data) { int progress = data["percent"]; if (progress != null) { setState(() { percent = progress; }); } }
2.4 Others
In addition, Android also has the issue of permission application. You can refer to the code in the following project.
/xushengjiang0/flutter_yuedu
dart code: lib/widget/update_version.dart
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.