【问题标题】:Navigator has no active routes to replaceNavigator 没有可替换的活动路线
【发布时间】:2020-12-29 22:46:47
【问题描述】:

我有一个运行良好的项目,但在最新的颤振核心更新后它没有,我得到:

E/flutter (4510): [错误:flutter/lib/ui/ui_dart_state.cc(166)] 未处理的异常:'package:flutter/src/widgets/navigator.dart': 断言失败:第 3582 行 pos 12: '_history.any(_RouteEntry.isPresentPredicate)': Navigator 没有 要替换的活动路线。 E/颤振(4510):#0
_AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:46:39) E/flutter (4510): #1 _AssertionError._throwNew (dart:core-patch/errors_patch.dart:36:5)

  Future<void> _submit() async {
    if (!_formKey.currentState.validate()) {
      return;
    }
    _formKey.currentState.save();

    final pref = await SharedPreferences.getInstance();
    pref.setString('ip', _ip);
    pref.setBool('isTablet', isTablet);
   pref.setBool('setting1', isActivated);

     
// error on next part of the code

      Navigator.of(context).pop(); 
      Navigator.of(context).pushReplacementNamed('/');

      Provider.of<Auth>(context, listen: false).logout();
  }

谢谢

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    根据您的问题,我假设您有 2 条路线。 1 表示包含您的提交功能的当前屏幕,然后另一个 1 是“/”。

    Navigator.of(context).pop(); // closes the current screen
    Navigator.of(context).pushReplacementNamed('/') // replaces the current screen
    

    因此,如果您确实弹出当前屏幕,代码中的下一行将给出错误,因为堆栈上没有您的 sn-p 将替换的路由。删除 Navigator.of(context).pop();它会解决你的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-25
      • 1970-01-01
      • 2018-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多