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 Data to a Stateful Widget in Flutter?
- How to Pass Data to a Stateful Widget In Flutter ?
To pass data to the stateful widget just pass parameters to the state using the constructor. You can easily access this parameter using the widget.yourPeram in your second stateful widget. Just follow the below tutorial.
- Pass Data to a Stateful Widget In Flutter
To pass data to the stateful widget just pass parameters to the state using the constructor. You can easily access this parameter using the widget.yourPeram in your second stateful widget. Just follow the below tutorial.
To pass data to the stateful widget just pass parameters to the state using the constructor. You can easily access this parameter using the widget.yourPeram in your second stateful widget. Just follow the below tutorial.
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("ad[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.
Leave a Reply