In Flutter, the user interface is displayed through widgets. The widget is used to represent the data to the user and allows user interaction.
In a mobile application, we can have multiple widgets(Screen) and that can interact with each other. During widget interaction, we might be required to pass data from one widget to other.
Example: If you are developing an application for news feed reader, you may require having at least two screens. While the first screen displaying the list of news feeds and other displaying the feed details. Sometimes we need to send data to another screen and after some calculation, we need it back.
How can send data to another screen?
To send data, we have to use Navigator to push a new MaterialPageRoute onto the stack with our data.
How can get back data?
We can send data back to the previous screen with the help of the pop method.
In this post, we going to create a Flutter application. Where we'll send some data to another screen and we'll receive updated data. To create this app, we have to use navigator and route that we have explained in our post: Screen push and pop with Navigator.
Creating a new Project
1. Create a new project from File ⇒ New Flutter Project with your development IDE.
2. Open main.dart file and edit it. As we have set our theme and change debug banner property of Application.
in the above code snippet, we have declared route of the app that we need for exchange data.
3. After that create the first screen of the app that is dashboardscreen.dart.
in the dashboard screen toolbar, we using a profile icon. From the dashboard screen, we sending a name (Developer) to the profile screen. In order to navigate to a profile screen, we use the Navigator.push() method. The push method requires a Route. So here, we created our own Route and using the MaterialPageRoute. It'll perform platform-specific transitions animation.
4. To retrieve the values in another screen. We need to add a parameterized constructor. So, we have created it in the profilescreen.dart file and put it in the field variable.
as you can see above, on the profile screen. We displaying name that's sent from the dashboard screen. In profile screen toolbar, we have created a setting icon. When you press on it, you will move on setting screen.
5. In the setting screen, we use a text field widget. The value of text field widget sends to profile screen back.
,)
When a route is used to return a value, the route’s type parameter should match the type of pop’s result. Here we need a String data back so we’ve used MaterialPageRoute<String>. As you can see, in the profilescreen.dart file. We parsing the result to display data.
Now merge all code and run the app. You'll see the final app look like below and app able to exchange data.
In a mobile application, we can have multiple widgets(Screen) and that can interact with each other. During widget interaction, we might be required to pass data from one widget to other.
Example: If you are developing an application for news feed reader, you may require having at least two screens. While the first screen displaying the list of news feeds and other displaying the feed details. Sometimes we need to send data to another screen and after some calculation, we need it back.
How can send data to another screen?
To send data, we have to use Navigator to push a new MaterialPageRoute onto the stack with our data.
How can get back data?
We can send data back to the previous screen with the help of the pop method.
In this post, we going to create a Flutter application. Where we'll send some data to another screen and we'll receive updated data. To create this app, we have to use navigator and route that we have explained in our post: Screen push and pop with Navigator.
Creating a new Project
1. Create a new project from File ⇒ New Flutter Project with your development IDE.
2. Open main.dart file and edit it. As we have set our theme and change debug banner property of Application.
in the above code snippet, we have declared route of the app that we need for exchange data.
3. After that create the first screen of the app that is dashboardscreen.dart.
in the dashboard screen toolbar, we using a profile icon. From the dashboard screen, we sending a name (Developer) to the profile screen. In order to navigate to a profile screen, we use the Navigator.push() method. The push method requires a Route. So here, we created our own Route and using the MaterialPageRoute. It'll perform platform-specific transitions animation.
4. To retrieve the values in another screen. We need to add a parameterized constructor. So, we have created it in the profilescreen.dart file and put it in the field variable.
as you can see above, on the profile screen. We displaying name that's sent from the dashboard screen. In profile screen toolbar, we have created a setting icon. When you press on it, you will move on setting screen.
5. In the setting screen, we use a text field widget. The value of text field widget sends to profile screen back.
,)
When a route is used to return a value, the route’s type parameter should match the type of pop’s result. Here we need a String data back so we’ve used MaterialPageRoute<String>. As you can see, in the profilescreen.dart file. We parsing the result to display data.
Now merge all code and run the app. You'll see the final app look like below and app able to exchange data.
I hope you got the basic idea of data exchange between the Flutter screen. If you facing any issue to achieve it, please feel free to ask it in the comment section below and you can ask it on SLACK.