Reputation: 93
Currently i am learning flutter and i am stucked in navigation. what i wanted to achieve is : A Variable which is global to all the screens : For Example There is a variable score on the main page which is initialize with zero and on main page there are four buttons which takes to four different screen and after clicking a button on main page the screen changes to another. Now in another screen we will perform something which updates the value of score.Now when we come back to main page the updated score should be reflected and also if we click on some other button which takes us to another screen,updated score should also be reflected there.
Upvotes: 0
Views: 1310
Reputation: 9625
You can do this in more than one way, but the simplest is using the Navigator class and passing the value you want to pass back to the parent widget through the .pop()
method to return to your first view, like this:
Navigator.of(context).pop(your_data_here);
On the parent class, where you used your Navigator to navigate to the other view, you can expect to receive the data:
RaisedButton(
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) {
return OtherView();
}
)).then((value_from_other_view){
// You can here use the data from the other view
});
},
)
Upvotes: 1