Member-only story
FLUTTER INTERVIEW QUESTIONS
Does State object live after dispose()?
…and what triggers its disposal?
Widgets are immutable, States are not. Every StatefulWidget has its own State object, which can change and user can see a change in it by calling setState().
‘setState() called after dispose()’ - we all have seen this multiple times and it means that State object has been removed permanently from the element tree(more about it here) and its UI can not be drawn on the screen anymore.
But… what happens to that State object after dispose has been performed? Is it still alive?
I’ll try to explain in this example:
I create simple column with Text and a Container(my DisposableWidget), that gets redrawn 5 times:
Each time new iteration is performed, old DisposableWidget is disposed and new one created. It listens to the stream events from the MainScreen and has some delayed future that will be finished after 10 seconds.