close

How to Pass Data to a Stateful Widget in Flutter?

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?

  1. 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.

  2. 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("[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

Leave a Comment

%d bloggers like this: