Member-only story
Flutter — How to pass data between screens in Flutter
This article will cover both passing data forward and passing data backward between widgets in Flutter.
Unlike Android Activities and iOS ViewControllers, different screens in Flutter are just widgets. Navigating between them involves creating something called a route and using the Navigator
to push and pop the routes on and off the stack.
Passing data forward to the next screen
To send data to the next screen you do the following things:
Step 1: Make the SecondScreen
constructor take a parameter for the type of data that you want to send to it. In this particular example, the data is defined to be a String
value and is set here with this.text
.
class SecondScreen extends StatelessWidget {
final String text;
SecondScreen({Key key, @required this.text}) : super(key: key);
...
Step 2: Then use the Navigator
in the FirstScreen
widget to push a route to the SecondScreen
widget. You put the data that you want to send as a parameter in its constructor.