In our previous tutorial, we have learnt how to send data from a screen to another. In this article we learn how to return or send back a data from a screen to its previous screen on page pop or screen back or close in Flutter.
Table of Contents
On First Screen
For getting this trick, you need to alter your second screen calling function into aync – await method, that is, first screen waits for a return data. So there should be a variable to keep return value.
String datafromSecondScreen = await Navigator.push(
context MaterialPageRoute(builder: (context) => SecondScreen()
setState(() {
textFromSecondScreen = datafromSecondScreen;
});
));
Where
- datafromSecondScreen is the variable which is ready to keep the value from second screen.
- setState() will handle the text from second screen. You can use it wherever you want.
In above test, code, the value datafromSecondScreen is assigned to variable textFromSecondScreen
On Second Screen
On second screen, while closing screen (pop or back) you need to specify the return value as a parameter.
Navigator.pop(context,returnData);
Send Data on Both Direction

If you want to send data both direction, that is sending a data to another screen and get back data when you back to previous screen, then try below code:
Full app code:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final appTitle = 'First Screen';
return MaterialApp(
title: appTitle,
home: Scaffold(
appBar: AppBar(
title: Text(appTitle),
),
body: BuildBody(),
),
);
}
}
class BuildBody extends StatefulWidget {
@override
_BuildBodyState createState() => _BuildBodyState();
}
class _BuildBodyState extends State<BuildBody> {
String textFromSecondScreen='';
TextEditingController txtController = TextEditingController();
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(20),
child: Column(
children: <Widget>[
Text(textFromSecondScreen,
style: TextStyle(
fontSize: 30
),),
Row(
children: <Widget>[
Text(" Type something: "),
new Flexible(
child: new TextField(
controller: txtController,
),
),
],
),
RaisedButton(
child: Text("Send to second screen"),
onPressed: (){
goToSecondScreen(context);
},
)
],
)
);
}
void goToSecondScreen(BuildContext context) async{
String dataFromSecondPage = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SecondScreen(txtController.text),
));
setState(() {
textFromSecondScreen = dataFromSecondPage;
});
}
}
class SecondScreen extends StatefulWidget {
final String mydata;
SecondScreen(this.mydata);
@override
_SecondScreenState createState() => _SecondScreenState(mydata);
}
class _SecondScreenState extends State<SecondScreen> {
TextEditingController secondTxtController = TextEditingController();
String mydata;
_SecondScreenState(this.mydata);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Second screen')),
body:Container(
child: Column(
children: <Widget>[
Text(mydata,
style: TextStyle(
fontSize: 30
),),
Row(
children: <Widget>[
Text(" Type something: "),
new Flexible(
child: new TextField(
controller: secondTxtController,
),
),
],
),
RaisedButton(
child: Text("Send to first screen"),
onPressed: (){
Navigator.pop(context, secondTxtController.text);
},
),
],
)
),
);
}
}
Be First to Comment