mtical
mtical

Reputation: 329

Flutter 2.0 pushAndRemoveUntil not working

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

Answers (6)

Giulia Santoiemma
Giulia Santoiemma

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

Saboor Khan
Saboor Khan

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

Kyaw San Oo
Kyaw San Oo

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

Jasmin Sojitra
Jasmin Sojitra

Reputation: 1301

Try this

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

Upvotes: 0

user14280337
user14280337

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

Alessandro Cignolini
Alessandro Cignolini

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

Related Questions