flutter navigation pop to different screen

154 views Asked by At

i use

Navigator.push(context, MaterialPageRoute(builder: (context) => B()));

to push from A to B to C to D i need to pop back from D to B i also need to remove D and C form stack i used

Navigator.popUntil(context, (route) => route is B);

but it gives me a blank screen it works only with initial route, sloutions i found is to use

Navigator.pop();

twice

is there any alternative solutions?

1

There are 1 answers

0
Jaecheol Park On

You can set route name.

Navigator.push(
  context, 
  MaterialPageRoute(
    settings: RouteSettings(name: '/B'),
    builder: (context) => B(),
  ),
);

When you call popUntil set parameter using this name.

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