user1187968
user1187968

Reputation: 7986

Flutter: pop all underlying pages in the screen stack

Let's say in Flutter, I have a page screen stack at follow. The screen is showing page D, the topmost page.

Page D
Page C
Page B
Page A

If I do Navigator.of(context).popUntil((route) => route.isFirst); on page D, the first line in the the build() method, it will only pop Page C and Page B.

How do I pop page A as well?

Upvotes: 1

Views: 959

Answers (2)

Adil Naseem
Adil Naseem

Reputation: 1441

From page A, you can call:

Navigator.of(context).pushNamedAndRemoveUntil(
                        A.routeName, (route) => route ==
                        A.routeName);

I think it's too late to answer your question but it may help others.

Upvotes: 0

GrahamD
GrahamD

Reputation: 3165

In Flutter the page routes are a stack. In your example D is the top of the stack and A is the bottom. If you pop down to A you have cleared the other three routes from the stack. If you then try to pop A, you will get a black screen because there will be no routes remaining in the stack. If you want to pop A and show D you will need to use Navigator.pushReplacementNamed(context, 'D');

Upvotes: 1

Related Questions