Hello Guys How are you all? Hope You all are fine. Sometimes we have to pass data between two screens Especially we need to pass data when we navigate the screen. So today Here I come with a tutorial on How to Pass Data to a Stateful Widget in Flutter?
Without Wasting your Time Lets start this tutorial.
How to Pass Value From One Screen to Another Screen Using Navigator in Flutter ?
How to Pass Data From One Screen to Another Using Navigator In Flutter
Question : How to pass Value from one screen to another screen using navigator in flutter ?
Answer : To pass data to another screen then just declare parameters to the second screen using the constructor. Then you can easily access this parameter using widget.yourParameter in your second screen. Just Follow Below Tutorial.
To pass data to another screen then just declare parameters to the second screen using the constructor. Then you can easily access this parameter using widget.yourParameter in your second screen. Just Follow Below Tutorial.
How to Pass Data From One Screen to Another Using Navigator In Flutter ?
1. Suppose I Have Screen Named LoginScreen When I am Login Into it. I Want To Redirect User To HomeScreen With Some Parameter Like Email.
2. So I Am Just Declaring Email Parameter In My HomeScreen Exact Like Below And Set parameters to State using it’s constructor.
class HomeScreen extends StatefulWidget {
final String email;
const HomeScreen(this.email);
@override
HomeScreenState createState() => HomeScreenState();
}
class HomeScreenState extends State<HomeScreen> {
}
3. Now I am Passing My Parameter In LoginScreen Navigator Exact Like Below.
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => App("[email protected]"),
),
);
4. Then I Can Access this email Parameter in my HomeScreenState Class As Like Below.
class HomeScreen extends StatefulWidget {
final String email;
const HomeScreen(this.email);
@override
HomeScreenState createState() => HomeScreenState();
}
class HomeScreenState extends State<HomeScreen> {
@override
Widget build(BuildContext context) {
return Text(widget.email); // Here you direct access using widget
}
}
Summery
So it’s all About All possible Methods. Hope this above all solution helped you a lot. Comment below Your thoughts and your queries. Comment Below your suggestion.
Check Out Below Article
- How to make a blur Background Image effect in Flutter using BackdropFilter.
- Create Rounded Corners Image in Flutter.
- How to Dynamically Disable and Enable Button in Flutter?
- set Background Image to Scaffold in Flutter.
- How To Select multiple images with Flutter
- How to Set Network Image In Circular Avatar In Flutter?