Trần Đức Tâm
Trần Đức Tâm

Reputation: 4443

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.

Upvotes: 32

Views: 40659

Answers (3)

dontknowhy
dontknowhy

Reputation: 2896

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

Upvotes: 4

Bansari Patel
Bansari Patel

Reputation: 51

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

Upvotes: 5

encubos
encubos

Reputation: 3313

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.


Upvotes: 51

Related Questions