user3249281
user3249281

Reputation: 535

Difference between 2 ways of passing data to Flutter route

i'm reviewing the flutter.dev tutorials. I'm a bit confused by 2 of their articles.

1) Send data to a new screen https://flutter.dev/docs/cookbook/navigation/passing-data

2) Pass arguments to a named route https://flutter.dev/docs/cookbook/navigation/navigate-with-arguments

To me, they more or less both accomplish the same thing but in different ways. It seems #1 passes the data using Navigator's "arguments" parameter and then pulls it out in the target widget via ModalRoute.of(context).settings.arguments. It seems #2 uses the target widget's constructor to receive the data. Am I missing something? When would I use one vs the other?

Thanks!

Upvotes: 9

Views: 1120

Answers (1)

Rémi Rousselet
Rémi Rousselet

Reputation: 277527

There are two main differences:

  • push vs pushNamed, which means dynamic vs static routes.
  • Who creates the Route subclass. Using push, it's the widget that calls Navigator.push whereas using pushNamed, it's MaterialApp/CupertinoApp or onGenerateRoute.

This has an impact on features such as transitions between routes, separation of concern, or deep links.

Upvotes: 12

Related Questions