Flutter 2.0 pushAndRemoveUntil not working

Viewed 2412

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.

4 Answers

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);

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('/'),

Try this

Navigator.pushAndRemoveUntil(
    context, MaterialPageRoute(builder: (context) => SignInPage()), (
    route) => false);

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()),
);
Related