Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter what is the Difference between using `pushReplacementNamed` and `pushReplacement` when pushing a new route?

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?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading