flutter - 使用当前和所需的内部版本号执行应用程序强制更新
问题描述
我想强制更新我的应用程序。
这是我到目前为止所做的。
- 使用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");
}
}
推荐阅读
- anychart - 阻止调用 static.anychart.com 和 logo.png 加载
- javascript - Firebase 存储上传多个文件并一次获取它们的下载 URL
- python - Photutils/IRAF Starfinder:给出总数
- reactjs - 如何在 asp dot net web API 和 ReactJS 作为客户端使用 IP 连接的摄像头捕获图像
- python - 在 python 中的字符串列表中查找字符串对我不起作用
- jquery - 开始和结束参数 Fullcalendar 中的奇怪日期
- field - 如何删除在 MarkLogic 中创建的所有字段
- r - 错误:未能获得原语“c”的泛型
- react-native - 如何避免键盘在本机反应中推动整个布局?
- node.js - 如何在 Azure 上启用 ssh 连接