例外:错误状态:未来已经完成

问题描述

在主页的初始状态检查版本不匹配时,我遇到了上述错误, 这是我的代码

 @override
  void initState() {
    getCategoriesName();

    checkInternetConnection();

    try {
      versionCheck(context);
    } catch (e) {
      print(e);
    }
    super.initState();
  }

 versionCheck(BuildContext context) async {
    //Get Current installed version of app
    final PackageInfo info = await PackageInfo.fromPlatform();
    double currentVersion = double.parse(info.version.trim().replaceAll(".",""));
    print("Current Version--------------------");
    print(info.version);

    //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(".",""));

      print("New Version-----------------------");
      print(remoteConfig.getString('force_update_current_version'));
      if (newVersion > currentVersion) {
     _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 = "New Update Available";
        String message =
            "There is a newer version of app available please update it Now.";
        String btnLabel = "Update Now";
        String btnLabelCancel = "Later";
        return new AlertDialog(
          title: Text(title),content: Text(message),actions: <Widget>[
            FlatButton(
              child: Text(btnLabel),onpressed: () => _launchURL(PLAY_STORE_URL),),FlatButton(
              child: Text(btnLabelCancel),onpressed: () => Navigator.pop(context),],);
      },);
  }

每次我运行代码时,它都会打印 3 次我在检查版本不匹配时打印的当前版本和新版本。在搜索错误的所有可能原因后,我发现 showdialog 是罪魁祸首,我已经尝试了一切可能的方法来修复它,例如将 showdialog 包围在

WidgetsBinding.instance.addPostFrameCallback((_){


});

SchedulerBinding.instance.addPostFrameCallback((_) {



});

在上面的代码包围之后,我收到这个错误 Flutter:查找已停用小部件的祖先是不安全的

解决方法

我认为您需要从 try-catch 中删除 initState 块,然后尝试使用

 @override
  void initState() {
    getCategoriesName();
    checkInternetConnection();
    versionCheck(context);   // CHeck here
    super.initState();
  }

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...