Home > Mobile >  Flutter what is the Difference between using `pushReplacementNamed` and `pushReplacement` when pushi
Flutter what is the Difference between using `pushReplacementNamed` and `pushReplacement` when pushi

Time:03-25

I know that pushReplacementNamed takes the name of the route and passes it with the route settings to the onGenerateRoute with that name and pushes on the Navigator the page returned by that function, and I know that pushReplacement takes the page route directly as an argument, but what I am asking about is when should I use one instead of the other, is there a specific use case of each one or is it just a matter of preference?

CodePudding user response:

It's a matter of preference, but I prefer the pushReplacementNamed one. The main difference is the one you have already mentioned - the pushReplacementNamed goes through the onGenerateRoute callback and then returns the next route. This way it is easier to separate the navigation logic and keep it in a separate file. All your routes could be defined under onGenerateRoute method, your UI code is not tightly coupled with specific pages - you can just simply pass the route name. And that's also the reason why pushReplacementNamed contains the arguments parameter - since the next route does not know where it was called from, you can pass the needed data using the parameter.

  • Related