Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between pushReplacementNamed and popAndPushNamed in Flutter?

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.

like image 928
Trần Đức Tâm Avatar asked Jan 20 '20 03:01

Trần Đức Tâm


People also ask

What is Pushreplacementnamed in flutter?

Returns a Future that completes to the result value passed to pop when the pushed route is popped off the navigator.

How do you use popAndPushNamed in flutter?

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.

How do you use Pushandremoveuntil?

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.


3 Answers

Just a shorter answer.

The difference is only in the animation flutter executes.

  • pushReplacementNamed -->> "enter animation"
  • popAndPushNamed -->> "exit animation"

User A R post this also in his answer.

pushReplacementNamed will execute the enter animation and popAndPushNamed will execute the exit animation.


like image 127
encubos Avatar answered Oct 22 '22 18:10

encubos


@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.

like image 27
roun paleum Avatar answered Oct 22 '22 18:10

roun paleum


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

like image 2
Bansari Patel Avatar answered Oct 22 '22 18:10

Bansari Patel