flutter - 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();
}
谢谢
解决方案
根据您的问题,我假设您有 2 条路线。1 表示包含您的提交功能的当前屏幕,然后另一个 1 是“/”。
Navigator.of(context).pop(); // closes the current screen
Navigator.of(context).pushReplacementNamed('/') // replaces the current screen
因此,如果您确实弹出当前屏幕,代码中的下一行将给出错误,因为堆栈上没有您的代码段将替换的路由。删除 Navigator.of(context).pop(); 它会解决你的问题。
推荐阅读
- android - 无法在 android 中使用 AWS 创建组
- php - 在控制器上处理时有没有办法获取数据 - Laravel PHP
- php - 在 Plesk REST API 中创建邮件帐户
- ssh - 由于您的组,如何确定您是否可以使用 SFTP 写入目录?
- mongodb - 在 express.js POST 方法中没有被调用
- sql - T-SQL - 我如何得到这个结果集
- sql - 将两个表与一个表中的附加字段连接起来
- android - 杀死 Runnable 进程
- c# - 如何在 Window 中重新定位 UserControl(s)
- google-bigquery - 如何估计 BigQuery 中表的复制时间?