首页 > 解决方案 > 我想更新 Playstore 中的应用版本以向用户显示消息对话框

问题描述

我是新手。我想在 Playstore 中更新新版本的应用程序,以向用户显示消息对话框以更新新版本,我使用了插件 version_check 0.2.0。当用户已经更新,但它仍然显示相同的消息对话框。更新后如何不显示消息对话框。谁能帮助我?

这是我的代码

这是我的代码

这是我的代码

标签: flutter

解决方案


由于问题中的一切都不清楚,您应该按照给定的步骤来实现相同的目标。

步骤 1. 转到 Firebase 中的远程配置并添加图像中显示的一些参数,然后发布它。在此处输入图像描述

步骤 2. 创建函数 VersionCheck 和 _showVersionDialog,如下所示:

versionCheck(){
    //Get Current installed version of app

    WidgetsBinding.instance.addPostFrameCallback((_) async {
      final PackageInfo info = await PackageInfo.fromPlatform();
      double currentVersion = double.parse(info.version.trim().replaceAll(".", ""));

      //Get Latest version info from firebase config
      final RemoteConfig remoteConfig = await RemoteConfig.instance;

      try {
        // Using default duration to force fetching from remote server.
        await remoteConfig.fetch(expiration: const Duration(seconds: 0));
        await remoteConfig.activateFetched();
        remoteConfig.getString('force_update_current_version');

        double newVersion = double.parse(remoteConfig
            .getString('force_update_current_version')
            .trim()
            .replaceAll(".", ""));
        if (newVersion > currentVersion) {
          setState(() {
            versionCode =  remoteConfig.getString('force_update_current_version');
            aboutVersion =  remoteConfig.getString('update_feature');
          });
          _showVersionDialog(context);
        }
      } on FetchThrottledException catch (exception) {
        // Fetch throttled.
        print(exception);
      } catch (exception) {
        print('Unable to fetch remote config. Cached or default values will be '
            'used');
      }

    });

  }
_showVersionDialog(context) async {
    await showDialog<String>(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        String title = "Update Available";
        String message =
            "About Update: \n";

        return ButtonBarTheme(
          data:   ButtonBarThemeData(alignment: MainAxisAlignment.center),
          child: new AlertDialog(
            shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(30)),
            title: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(title),
                Text("v"+versionCode),
              ],
            ),
            content: Container(
              height: 80,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(message,style: TextStyle(fontWeight: FontWeight.bold),),
                  Text(aboutVersion),
                ],
              ),
            ),
            actions: <Widget>[
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: RaisedButton(
                  child: new Text(
                    'Update',
                    style: TextStyle(color: Colors.white),
                  ),
                  color: Color(0xFF121A21),
                  shape: new RoundedRectangleBorder(
                    borderRadius: new BorderRadius.circular(30.0),
                  ),
                  onPressed: () {
                    _launchURL(PLAY_STORE_URL);
                  },
                ),
              ),
            ],
          ),
        );
      },
    );
  }

步骤 3. 在主屏幕的 init 函数中调用 VersionCheck,如下所示。


  @override
  void initState() {
  
    Future.delayed(const Duration(milliseconds: 5000), () {


       if(mounted){
         setState(() {
           versionCheck();
         });
       }

    });

    super.initState();

  }

第 4 步。每当您希望更新对话框出现在屏幕上时,只需将 Firebase 远程配置中的版本代码值增加为您的实际版本代码值即可。

这将帮助你实现你想要的。


推荐阅读