The NavigatorState
class in Flutter#navigator.dart
have 2 method with the similar behavior. What is the difference between pushReplacementNamed
and popAndPushNamed
in Flutter?
pushReplacementNamed
Replace the current route of the navigator by pushing the route named [routeName] and then disposing the previous route once the new route has finished animating in.
popAndPushNamed
Pop the current route off the navigator and push a named route in its place.
Returns a Future that completes to the result value passed to pop when the pushed route is popped off the navigator.
Pop and Push Named It can be done using Navigation. popAndPushNamed with context as the first argument. The animations for the pop and the push are performed simultaneously, so the route below (in this case HomeScreen may be briefly visible even if both the old route and the new route are opaque.
Push the given route onto the navigator that most tightly encloses the given context, and then remove all the previous routes until the predicate returns true. The predicate may be applied to the same route more than once if Route.
Just a shorter answer.
The difference is only in the animation flutter executes.
User A R post this also in his answer.
pushReplacementNamed will execute the enter animation and popAndPushNamed will execute the exit animation.
@A R's explanation was great, but it was not a practical answer.
Unless there is only one stack, the effects of the two are perfectly the same.
This is because the app is terminated when popPushedNamed is used when the stack is 1.
And as @encubos said, the animation effect seems to be different.
In pushReplacementNamed, the current route of the navigator pushes the route named [routeName] and then dispose of the previous route once the new route has finished animating Whereas in popAndPushNamed, the current route gets popped out first then the new route gets pushed in it does not wait for the animation of the other route to get completed
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With