Flutter: Send data with Navigation Pop – Back Button

Send data from the second page to backwards (first page). We use navigation from page 1 to page 2. From the page 2 while we come back we have to receive data from page 2 to page 1.

Page 1 Code:

FlatButton(
                    onPressed: () async{
                     var typedName = await Navigator.push(
                          context,
                          MaterialPageRoute(builder: (context) => CityScreen()));
                          print(typedName);
                          if(typedName != null){
                            var weatherData = await weather.getCityWeather(typedName);
                            updateUI(weatherData);
                          }
                    },

Page 2 Code:

              Container(
                padding: EdgeInsets.all(20.0),
                child: TextField(
                  style: TextStyle(color: Colors.black),
                  decoration: kTextFieldInputDecoration,
                  onChanged: (value){
                    cityName = value;
                  },
                ),
              ),
              FlatButton(
                onPressed: () {
                  Navigator.pop(context, cityName);    //sending data to page 1
                },

About the Author: smartcoder

You might like

Leave a Reply

Your email address will not be published. Required fields are marked *