使用当前和所需的内部版本号执行应用程序强制更新

问题描述

我想强制更新我的应用。

这是我到目前为止所做的。

  1. 使用 package_info_plus
  2. 获取我的应用的当前构建版本
  3. 获得了我存储在 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");
  }
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...