0

So I want a user to be able to go back to the '/' named route after he/she reaches upto a certain page. The condition is that when that page is reached, user should be routed to '/' only on Navigator.pop(context).

ie., Lets say I have routes configured such that :

User is at '/' > Clicks a button and pushNamed to '/abc' > Clicks a button and pushNamed to '/xyz' > Clicks a button and pushNamed to '/mnq'

Now, I want that when user is at /mnq and Navigator.pop(context) is executed, user should be sent back to '/' and not anywhere else.

When I'm at /xyz, I tried to do a Navigator.pushNamedAndRemoveUntil(context, '/mnq', ModalRoute.withName('/'), arguments:....) but It does not work because when Navigator.pop is executed after reaching /mnq, I see a black screen.

Is there a way to do this. Please note that I need to send arguments from /xyz to /mnq.

Divyam Dhadwal
  • 395
  • 3
  • 19

3 Answers3

0

You can try use pushReplacementNamed()

'/' pushNamed to '/abc' pushReplacementNamed to '/xyz' pushReplacementNamed to '/mnq'

read this article for more information

Xuuan Thuc
  • 2,340
  • 1
  • 5
  • 22
0

pushNamedAndRemoveUntil is remove all page before. when you use back/pop, it will show black because no page before. you can pass argument with pushReplacementNamed Flutter - Pass values between routes using pushReplacementNamed or store data in local https://pub.dev/packages/shared_preferences

anggadaz
  • 342
  • 3
  • 13
0

1st. on pressing a button to user should be sent back to '/'

simply use Navigator.popUntil(context, ModalRoute.withName('/'));

in place of Navigator.pop(context);

2nd. if user tab back button

WillPopScope(
      onWillPop: () async {
        Navigator.popUntil(context, ModalRoute.withName('/'));
        return false;
      },
 child: Scaffold(......),
)
Fedor
  • 17,146
  • 13
  • 40
  • 131