I need to show a snackbar when an api call returns. The problem is that I pop the route before this api call is done. So I get the following error:
[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: Looking up a deactivated widget's ancestor is unsafe.
At this point the state of the widget's element tree is no longer stable.
To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.
This is my code:
ElevatedButton(
onPressed: () async {
final snackBar = SnackBar(
behavior: SnackBarBehavior.fixed,
duration: Duration(seconds: 2),
content: Text('Done'),
);
apiCall().whenComplete(() => ScaffoldMessenger.of(context).showSnackBar(snackBar));
Navigator.pop(context);
},
child: Text('Fetch data'),
),
How can I show a snackbar when the api call is done after the route is popped?