Reputation: 329
Not sure how to clear the stack and route to a new page after implementing the Flutter 2.0 routing.
The following isn't working:
Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (context) => SignInPage()), (route) => false);
[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.
Upvotes: 11
Views: 10543
Reputation: 312
This code solved the same problem in my case (it also works on web app):
Navigator.of(context, rootNavigator: true).pushAndRemoveUntil(
MaterialPageRoute(
builder: (context) => SignInPage(),
),
ModalRoute.withName('/'),
);
ModalRoute.withName('/')
removes all other routes.
Upvotes: 0
Reputation: 39
when I implement route for android push Notification and use pushAndRemoveUntil with
MaterialApp.router
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(builder: (context) => screen),
(Route<dynamic> route) => false);
then not work so make (Route route) => false to true
MaterialApp.router
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(builder: (context) => screen),
(Route<dynamic> route) => true);
Upvotes: 1
Reputation: 176
You can access your MaterialApp navigator with rootNavigator: true. See https://api.flutter.dev/flutter/widgets/Navigator/of.html. Try the following it is working.
Navigator.of(context, rootNavigator:
true).pushAndRemoveUntil(MaterialPageRoute(builder: (context) =>
SignInPage()), (route) => false);
Upvotes: 6
Reputation: 1301
Try this
Navigator.pushAndRemoveUntil(
context, MaterialPageRoute(builder: (context) => SignInPage()), (
route) => false);
Upvotes: 0
Reputation:
To completely remove the stack, which disallows the user from navigating to the previous screen, you should use PushReplacement
.
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (context) => SigInPage()),
);
Upvotes: -1
Reputation: 61
i think you need to pass the root name aswell.
Check my code i hope it might help you
Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: (BuildContext context) => SignInPage()), ModalRoute.withName('/'),
Upvotes: 0