Flutter 2.0 PushAndRemoveUntil 不起作用

mti*_*cal 8 dart flutter flutter-navigation

不知道实现 Flutter 2.0 路由后如何清除堆栈并路由到新页面。

以下内容不起作用:

Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (context) => SignInPage()), (route) => false);
Run Code Online (Sandbox Code Playgroud)

[VERBOSE-2:ui_dart_state.cc(186)] Unhandled Exception: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 3075 pos 7: '!hasPage || isWaitingForExitingDecision': A page-based route cannot be completed using imperative api, provide a new list without the corresponding Page to Navigator.pages instead.

小智 5

您可以使用 rootNavigator: true 访问您的 MaterialApp 导航器。请参阅https://api.flutter.dev/flutter/widgets/Navigator/of.html。尝试以下操作,它有效。

Navigator.of(context, rootNavigator: 
true).pushAndRemoveUntil(MaterialPageRoute(builder: (context) => 
SignInPage()), (route) => false);
Run Code Online (Sandbox Code Playgroud)


小智 0

我认为您还需要传递根名称。

检查我的代码我希望它可以帮助你

Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: (BuildContext context) => SignInPage()), ModalRoute.withName('/'),
Run Code Online (Sandbox Code Playgroud)