I’ve implemented an app on Flutter framework. I’m new to flutter and I don’t know how to pass the data to new screen. Someone said me that you should use Named routes in your app so I tried it. But now I don’t know how to pass the data from one screen to another screen.
These are the routes in my MaterialApp:
routes: {
'/': (context) => FirstPage(),
'/second': (context) => SecondPage(),
'/third': (context) => ThirdPage(),
},
And this is how I navigate to new screen normally:
onPressed: () {
Navigator.pushNamed(context, '/second');
},
I want to pass the data to new screen when navigate to new screen.
2
Answers
You just need to pass the arguments. First in the file where you want your pass the data extract the argument like this:
Now where you navigate the page just pass the arguments like this:
All set. I hope this helps you. Happy Coding…
There are different ways to pass data from one screen to another but to pass data between screens using named routes in Flutter, you can utilise the arguments parameter in Navigator.pushNamed. Here’s how you can do it:
First, define your routes with arguments in your MaterialApp:
Now, when navigating to the second screen (SecondPage), you can pass data along with the route:
In the SecondPage widget, you can retrieve the data using ModalRoute.of(context).settings.arguments: