- 获取当前app的版本、平台信息
static String _version; static String _flatform;
import ‘package:package_info/package_info.dart’;
/// 获取当前版本
PackageInfo packageInfo = await PackageInfo.fromPlatform();
_version = packageInfo.version;
import ‘package:device_info/device_info.dart’;
/// 获取平台信息
Future getFlatForm() async {
if (Platform.isAndroid) {
_flatform = ‘android’;
} else {
_flatform = ‘ios’;
}
return _flatform;
}
- 从服务
器拉取app版本的最新信息
Map _data = await _fetchVersionInfo();
/// 拉取版本号信息
Future
HttpResult res = await Api.get(’/version-api/versions/latest’, query: {
‘from’: _flatform,
‘version’: _version,
});
return res.code == 200 ? res.data : null;
}
- 与本地version信息比对,选择是否展示更新弹窗,我们采用的是event_bus触发
if (localVersion == remoteVersion) return;eventManager.eventBus.fire(new UpdateAppEvent(versionInfo));
- 进行版本升级,要注意区分Android与IOS (1) IOS更新app包
IOS的处理方式比较简单,直接跳转到appStore即可,我这里采用的urlLauncher直接跳转
urlLauncher.launch(_link);
(2) Android更新app包
需要开启存储权限,如果没有权限就申请
import ‘package:permission_handler/permission_handler.dart’;
/// 检查是否有权限,用于安卓
Future checkPermission() async {
if (_flatform == ‘android’) {
PermissionStatus permission = await PermissionHandler()
.checkPermissionStatus(PermissionGroup.storage);
if (permission != PermissionStatus.granted) {
Map<PermissionGroup, PermissionStatus> permissions =
await PermissionHandler()
.requestPermissions([PermissionGroup.storage]);
if (permissions[PermissionGroup.storage] == PermissionStatus.granted) {
return true;
}
} else {
return true;
}
} else {
return true;
}
return false;
}
需要在Android的 AndroidManifest.xml文件增加权限配置
下载apk根据返回的下载链接,需要先把Android包文件下载到本地,这里需要对文件流进行操作,下载工具我是采用的HTTP请求工具库dio,这里也可以采用专业的下载插件flutter_downloader,这个插件支持Android、IOS下载,但是配置起来复杂,我折腾了好长时间,也没能配置成功,有玩转这个插件的可以给我推荐些文章。
import ‘package:dio/dio.dart’;
import ‘package:path_provider/path_provider.dart’;
/// 下载安卓更新包
Future downloadAndroid(String url) async {
/// 创建存储文件
Directory storageDir = await getExternalStorageDirectory();
String storagePath = storageDir.path;
File file = new File(’ s t o r a g e P a t h / storagePath/ storagePath/{Config.APP_NAME}v${_version}.apk’);
if (!file.existsSync()) {
file.createSync();
}
try {
/// 发起下载请求
Response response = await Dio().get(url,
onReceiveProgress: showDownloadProgress,
options: Options(
responseType: ResponseType.bytes,
followRedirects: false,
));
file.writeAsBytesSync(response.data);
return file;
} catch (e) {
print(e);
}
}
安装apk
import ‘package:install_plugin/install_plugin.dart’;
/// 安装apk
Future installApk(String url) async {
File _apkFile = await downloadAndroid(url);
String _apkFilePath = _apkFile.path;
if (_apkFilePath.isEmpty) {
;
}
}
安装apk
import ‘package:install_plugin/install_plugin.dart’;
/// 安装apk
Future installApk(String url) async {
File _apkFile = await downloadAndroid(url);
String _apkFilePath = _apkFile.path;
if (_apkFilePath.isEmpty) {