如何解决使用当前和所需的内部版本号执行应用程序强制更新
我想强制更新我的应用。
这是我到目前为止所做的。
- 使用 package_info_plus 获取我的应用的当前构建版本
- 获得了我存储在 firebase 远程配置中的强制构建版本。所以我使用了这个包:firebase_remote_config
然后我比较了两个版本号以查看是否需要更新。之后我该怎么办?
这是我的代码:
void initState(){
super.initState();
checkForUpdate();
_initPackageInfo();
_enforcedVersion();
if(int.parse(_packageInfo.buildNumber) > int.parse(enforcedBuildNumber))
{
//How to force update?
}
}
Future<void> _initPackageInfo() async {
final info = await PackageInfo.fromPlatform();
setState(() {
_packageInfo = info;
});
}
Future<void> _enforcedVersion() async {
final RemoteConfig remoteConfig = RemoteConfig.instance;
await remoteConfig.setConfigSettings(RemoteConfigSettings(
fetchTimeout: const Duration(seconds: 10),minimumFetchInterval: Duration.zero,));
await remoteConfig.fetchAndActivate();
setState(() {
enforcedBuildNumber = remoteConfig.getString('enforced_build_number');
});
}
解决方法
您可以显示一个不可关闭的对话框,要求用户使用重定向按钮更新应用程序到设备应用商店。
通过使用诸如 url_launcher 之类的包,您可以轻松做到这一点:
代码示例
import 'dart:io' show Platform;
import 'package:url_launcher/url_launcher.dart';
// You can show a dialog like this
showDialog(
context: context,barrierDismissible: false,builder: (_) => AlertDialog(
title: Text('Please update your app'),actions: [
TextButton(
onPressed: launchAppStore,child: Text('Open App Store'),),],);
// Method to open the appstore
void launchAppStore() {
/// Depending on where you are putting this method you might need
/// to pass a reference from your _packageInfo.
final appPackageName = _packageInfo.packageName;
if (Platform.isAndroid) {
launch("https://play.google.com/store/apps/details?id=$appPackageName");
} else if (Platform.isIOS) {
launch("market://details?id=$appPackageName");
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。