Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between 2 ways of passing data to Flutter route

Tags:

flutter

dart

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!

like image 504
user3249281 Avatar asked Jul 24 '19 15:07

user3249281


People also ask

What are the different ways to create navigation in Flutter?

Flutter provides two types of APIs for navigation: imperative and declarative.

What are routes in Flutter?

In flutter, the pages or screens are called Routes. In android, these pages/screens are referred to as Activity and in iOS, it is referred to as ViewController. But, in a flutter, routes are referred to as Widgets. In Flutter, a Page / Screen is called a Route.


1 Answers

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.

like image 114
Rémi Rousselet Avatar answered Sep 21 '22 13:09

Rémi Rousselet