I got it resolved by others. The solution is basically to use the WillPopScope widget to set all “localranks” in widget.cities.attractions to zero when the back button is pressed. For that, we have to define the method in the Widget class and then to use forEach to access each attraction and set the localrank to zero from there.
This is the code:
Future<bool> moveToLastScreen() async {
setState(() {
widget.cities.attractions.forEach((attraction) => attraction.localrank = 0);
});
Navigator.of(context).pop();
return true;
}
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () => moveToLastScreen(),
child: Scaffold(...),
);
}
CLICK HERE to find out more related problems solutions.