flutter 应用自动更新
话不多说直接上代码:
package_info: ^0.4.0+16 path_provider: ^1.6.5 open_file: ^3.0.1 url_launcher: ^5.4.2
_update() async { if (Platform.isIOS) { // String url = // 'itms-apps://itunes.apple.com/cn/app/id414478124?mt=8'; // 这是微信的地址,到时候换成自己的应用的地址 // if (await canLaunch(url)) { // await launch(url); // } else { // throw 'Could not launch $url'; // } } else if (Platform.isAndroid) { String url = HttpUtils.VERSION_URL + 'app-release.apk'; try { /// 创建存储文件 Directory storageDir = await getExternalStorageDirectory(); String storagePath = storageDir.path; File _apkFile = await downloadAndroid(storagePath,url); OpenFile.open("${storagePath}/app-release.apk"); } catch (e) { print('${e}'); } } } /// 下载安卓更新包 Future<File> downloadAndroid(String storagePath,String url) async { File file = new File('$storagePath/app-release.apk'); if (!file.existsSync()) { file.createSync(); } try { print("-------------333333333333"); Function tr = throttle((List args){ print("-----------------------6666"); int count = args[0]; int total = args[1]; double radio = count/total; String str = (_progress_val*100).toInt().toString(); setState(() { _progress_val = radio; _progress = "下载进度:${str}%"; }); }, 500); /// 发起下载请求 Response response = await Dio().get(url, onReceiveProgress: (int count,int total){ // tr([count,total]); double radio = count/total; String str = (_progress_val*100).toInt().toString(); setState(() { _progress_val = radio; _progress = "下载进度:${str}%"; }); }, options: Options( responseType: ResponseType.bytes, followRedirects: false, )); file.writeAsBytesSync(response.data); return file; } catch (e) { print(e); } }