How to dispose of my Stateful Widget completely?
The above exception is shown when we call setState()
after we dispose of the widget due to async.
Handle this by using the property of flutter => mounted
mounted
tells about the availability of the widget.
...
if (mounted) {
setState(() {
...
});
}
...
I assume the problem is caused by the response from the server arrives after the widget is disposed.
Check if the widget is mounted before you call setState
. This should prevent the error you are seeing:
// Before calling setState check if the state is mounted.
if (mounted) {
setState (() => _noDataText = 'No Data');
}