I'm getting a warning in one of my Flutter apps in a method that calls an async service.
void _refreshAllOutcomes() {
confirmPopup(context, 'Refresh all outcomes?').then( (value) {
if (value) {
ScaffoldMessenger.of(context).showSnackBar(GdSnackbars.build(
'Habit tracking', 'Please wait - updating all tracking outcomes', ContentType.help));
ref.read(habitServiceProvider).refreshAllTrackingOutcomes(_getHabitId()).then( (_) {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
});
}
});
}
The logic of the method covers:
- Having the user confirm the request in a pop up message
- Display a snackbar pop up before the async service call starts
- initiate the async service call
- When the async service call completes, close the snackbar popup
This code is generating a compile warning that the 'BuildContext' should not be used across async calls.
How should I refactor my code to address this?
Note: I added a mounted check and the compile warning remains.
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(GdSnackbars.build(
'Habit tracking', 'Please wait - updating all tracking outcomes', ContentType.help));
}