I have the following code in flutter where I'm first getting the data into the page:
Future<void> _getGoalsList() async {
final response =
await http.post(Uri.parse('http://10.0.2.2:1111/goal/getGoals/1'));
final responseData = jsonDecode(response.body);
final goalsList = responseData['getGoals'];
goalsList.forEach((goal) {
goals.add(Goals(
id: goal['id'],
goal_name: goal['goal_name'],
goal_picture: goal['goal_picture']));
});
setState(() {
_goalList = goals;
});
}
@override
void initState() {
super.initState();
_getGoalsList();
}
And in this same page I have a button for going to a page to add new goals:
FloatingActionButton.small(
onPressed: () {
Navigator.pushNamed(context, '/addGoals');
},
),
Now in my add goals page I have a confirm button that takes me back to the previous page:
ElevatedButton(
onPressed: () {
_addGoalToList();
Navigator.pop(context);
},
child: Text('Confirm'),
),
Now I want the _getGoalsList API to be called whenever the user clicks confirm so it can fetch the new data
You can add a boolean to check whether data has been added to list or not.
Based on the data you can pass assign the value to isUpdated
variable.
ElevatedButton(
onPressed: () {
bool isUpdated = false;
_addGoalToList();
isUpdated = true;
Navigator.pop(context,isUpdated);
},
child: Text('Confirm'),
),
In your list page, simply check for the result of page.
FloatingActionButton.small(
onPressed: () async{
final result = await Navigator.pushNamed(context, '/addGoals');
if(result = true){
// code to update the list
_getGoalsList();
}
},
),