Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference Between Deactivate and Dispose?

In Flutter, StatefulWidget has dispose() and deactivate(). How are they different?

like image 269
Michael Yuwono Avatar asked May 31 '19 00:05

Michael Yuwono


People also ask

What is deactivate in flutter?

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.

What is the use of Dispose in flutter?

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.

What is void dispose?

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.

Why should we dispose controller in flutter?

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.


1 Answers

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.

like image 191
Rémi Rousselet Avatar answered Oct 18 '22 19:10

Rémi Rousselet