微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决例外:错误状态:未来已经完成

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

 @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();
  }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?