首页 > 解决方案 > 检查 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
              ));
            });
          }

标签: flutter

解决方案


如果您正在清除整个堆栈并显示特定屏幕,您应该尝试以下代码:

Navigator.of(context).pushNamedAndRemoveUntil('/screen4', (Route route) => false);

它应该做的工作。


推荐阅读