0

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

 @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: Find up a deactivated widget's parents is unsafe

4

1 回答 1

0

我认为您需要从中删除try-catchinitState,然后尝试

 @override
  void initState() {
    getCategoriesName();
    checkInternetConnection();
    versionCheck(context);   // CHeck here
    super.initState();
  }
于 2021-02-01T04:42:50.147 回答