首页 > 解决方案 > Navigator 没有可替换的活动路线

问题描述

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

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

谢谢

标签: flutterdart

解决方案


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

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

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


推荐阅读