In Flutter, StatefulWidget has dispose()
and deactivate()
. How are they different?
deactivate method Null safetyTransition from the "active" to the "inactive" lifecycle state. The framework calls this method when a previously active element is moved to the list of inactive elements. While in the inactive state, the element will not appear on screen.
Dispose is a method triggered whenever the created object from the stateful widget is removed permanently from the widget tree. It is generally overridden and called only when the state object is destroyed. Dispose releases the memory allocated to the existing variables of the state.
void dispose() Called when this object is removed from the tree permanently. The framework calls this method when this State object will never build again. After the framework calls dispose, the State object is considered unmounted and the mounted property is false. It is an error to call setState at this point.
dispose method used to release the memory allocated to variables when state object is removed. For example, if you are using a stream in your application then you have to release memory allocated to the stream controller. Otherwise, your app may get a warning from the PlayStore and AppStore about memory leakage.
dispose
is definitive. deactivate
is not.
deactivate
is called when a widget may be disposed. But that is not guaranteed.
A typical situation where deactivate
is called but not dispose
,is when moving widgets in the widget tree using a GlobalKey
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With