flutter - 检查 popUntil 是否可能/检查导航堆栈中是否命名路由
问题描述
如何检查workout
我的导航堆栈中是否有带有名称的路线?每当我调用popUntil
并且路由不在我的堆栈中时,我都会得到:
════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building Navigator-[GlobalObjectKey<NavigatorState> _WidgetsAppState#9d229](dirty, state: NavigatorState#86712(tickers: tracking 2 tickers)):
'package:flutter/src/widgets/navigator.dart': Failed assertion: line 2330 pos 12: '!_debugLocked': is not true.
Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
https://github.com/flutter/flutter/issues/new?template=BUG.md
The relevant error-causing widget was
MaterialApp
lib/main.dart:58
When the exception was thrown, this was the stack
#2 NavigatorState.build
package:flutter/…/widgets/navigator.dart:2330
#3 StatefulElement.build
package:flutter/…/widgets/framework.dart:4334
我已经尝试过了,但popUntil
仍然抛出异常
try {
Navigator.popUntil(context, ModalRoute.withName('workout'));
} catch (e) {
Future.delayed(Duration(milliseconds: 1000), () {
Navigator.push(context, MaterialPageRoute(
builder: (_context) => WorkoutPage(),
settings: RouteSettings(name: 'workout'),
fullscreenDialog: true
));
});
}
解决方案
如果您正在清除整个堆栈并显示特定屏幕,您应该尝试以下代码:
Navigator.of(context).pushNamedAndRemoveUntil('/screen4', (Route route) => false);
它应该做的工作。
推荐阅读
- ios - “UIAccessibilityNotifications”(又名“UInt32”)类型的值没有成员“screenChanged”
- reactjs - 同一页面中的多种语言
- javascript - 如何在单击的其他元素之后在列表中添加元素并具有特定值的数据属性?用jQuery
- xml - 如何解析 Html 以对应 XML 标准?
- javascript - 如何使用firebase-admin在firestore中将日期保存为时间戳?
- sql - 基于 TOP 的 SQL UPDATE
- c# - 如何通过确定深度级别将子节点添加到树视图?
- three.js - Three.js 的 CopyShader 是什么?
- amazon-web-services - AWS 凭证文件多个角色相同的配置文件
- javascript - 由于拖动元素中的点,JavaScript 拖放时的 XY 位置已关闭