简体   繁体   中英

Exception: bad state: Future already completed

While checking version mismatch in init state of home page I'm getting above error, Here is my code:

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

Everytime I run code it prints current version and new version 3 times which I've printed while checking version mismatch. After searching every possible cause for the error I found showdialog is culprit, I've tried every possible way to fix it like, surrounding showdialog in

WidgetsBinding.instance.addPostFrameCallback((_){


});

OR

SchedulerBinding.instance.addPostFrameCallback((_) {



});

After surrounding in above code, I'm getting this error Flutter: Looking up a deactivated widget's ancestor is unsafe

I think you need to remove try-catch block from initState and then try it w

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM