[Flutter + Dart]

[Flutter]pushAndRemoveUntil 과 pushReplacement 의 차이

ddgoori 2023. 2. 8. 19:32

pushAndRemoveUntil and pushReplacement are two navigation methods in Flutter's Navigator widget.

 

pushAndRemoveUntil pushes a new route onto the navigator stack and removes all the routes below it until a specified route is encountered. It takes two arguments:

  1. The context in which the Navigator is mounted.
  2. A RoutePredicate specifies the route that should stop the removal process

Navigator.pushReplacement(
  context, 
  MaterialPageRoute(builder: (context) => NewRoute()),
);

 

pushReplacement pushes a new route onto the navigator stack and removes the current route from the stack.

 

 
Navigator.pushAndRemoveUntil(
  context, 
  MaterialPageRoute(builder: (context) => NewRoute()), 
  ModalRoute.withName('/'),
);

 

In short, pushReplacement only removes the current route, whereas pushAndRemoveUntil removes all the routes below it until a specified route is encountered.