I have two Screens. In the first one, there is a ListView
. In the first element I display the value of the variable selectedObject
.
When the ListTile
is pressed, a second Screen is opened. I want to update the selectedObject
value after returning from the second screen.
I need to assign the value of result
to the selectedObject
variable.
I think I have to call the setState
method but I don't know how.
Here is my code:
class _FilterTaskState extends State<FilterTask> {
List taskList;
String selectedObject = "initial value";
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: _buildAppBar(context, "AppBar Title"),
body: Center(
child: new ListView(
children: <Widget>[
new ListTile(
leading: new Icon(Icons.home, color: Colors.black),
title: new Text("Selected Object", style: styleTitle),
subtitle: new Text(selectedObject),
trailing: new Icon(Icons.play_arrow, color: Colors.black),
onTap: () => _navigateToFilterObject(context),
),
...
],
)
),
);
}
}
_navigateToFilterObject(BuildContext context) async {
final result = await Navigator.push(context,
MaterialPageRoute(builder: (context) => FilterObject()),);
/// I want to set the 'selectedObject' value
/// selectedObject = result;
}
On your FilterObject
widget returns the value when you select the item like this:
Navigator.of(context).pop(theValueYouWantToReceive);
And you will get the result inside result
variable:
final result = await Navigator.push(context, MaterialPageRoute(builder: (context) => FilterObject()),);
Final code
_navigateToFilterObject(BuildContext context) async {
final result = await Navigator.push(context,
MaterialPageRoute(builder: (context) => FilterObject()),);
//refresh the state of your Widget
setState(() {
selectedObject = result;
});
}
_navigateToFilterObject
must be inside your _FilterTaskState
class
Read about Navigator pop : https://docs.flutter.io/flutter/widgets/Navigator/pop.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With