How to pop the navigator two pages back?

Viewed 121

I need to leave the current screen and navigate back to the second to last screen.

void leaveStatefulWidget3(){
  Navigator.pop(context);
}

// StatefulWidget 2...

// StatefulWidget 3 {....}
// the current one (where the navigator is now)

My app has navigated from the first screen to 2, and then to 3.

I need to leave or Navigator.pop(context) the StatefulWidget 3 to stateFull 1.

NOTE : I have complete access to my leaveStatefulWidget3() method from StatefulWidget 3, but don't know how to specify the route to use Navigator.pop(context) in leaveStatefulWidget3().

When I use Navigator.pop(context) directly from the first widget it leaves stateFull 1.

2 Answers

just use Navigator.pop(context) twice

You could easily use 'popUntil' with 'pushNamed'

Add the route settings with route names when pushing, like this.

 Navigator.pushNamed(context, < StatefulWidget name >);

Then, at the recent widget ( in your case, its stateful_widget 3 )

Navigator.popUntil(context, ModalRoute.withName("/"));

*Here you can use widget name or '/' *'/' will bring you back to the startup widget

Or , if this is always from certain widget to very first one You could directly use

Navigator.of(context).popUntil((route) => route.isFirst);
Related