Answers for "Unhandled Exception: setState() called after dispose(): _SearchResultsWidgetState#649c3(lifecycle state: defunct, not mounted"

1

Unhandled Exception: setState() called after dispose(): _SearchResultsWidgetState#649c3(lifecycle state: defunct, not mounted

After an await, your widget may not be mounted anymore. Doing setState gives 
you an exception at that time. This is actually a good thing, the code that 
follows should not be executing anyway, since you are somewhere else.

You have three options about the "setState() called after dispose()" exception:

1. Safely ignore it. The exception is saving your function from continuing.

2. Place a if (!mounted) return; between each await and setState(). It may be a
good habit to put it after each await.

3. Replace your setState() calls with setStateIfMounted() and define it as:

void setStateIfMounted(f) {
  if (mounted) setState(f);
}

I explain these approaches in this video.
https://youtu.be/t8LgA4zcW6M?list=PLxcvsYzLfaTCH6RNIr7PyLrEZRlP6uKhn&t=2681
Posted by: Guest on August-05-2021

Code answers related to "Unhandled Exception: setState() called after dispose(): _SearchResultsWidgetState#649c3(lifecycle state: defunct, not mounted"

Browse Popular Code Answers by Language