What Stateful Widget is made of
by durlavk
Stateful widget and it’s lifecycle ♻️.
In previous post I described about stateful widgets. It consists of two classes. The StatefulWidget
class and the State
class. As stateful widget have state so they have some lifecycle method. Lifecycle in general terms is an order in which different part of the code will get executed. So, let’s see the stateful widget’s lifecycle methods.
- createState() -
createState()
is fromStatefulWidget
class and it creates the second classState
. - build() - common in all widget.
build()
renders the ui. - initState() - This is called when widget is created, after the constructor.
- didChangeDependencies() - It gets called when dependencies of the widget changes causing a new build.
- didUpdateWidget() - It gets called when state within
State
class changes and it needs to create a new Stateful Widget. - setState() - This is used to set state and it causes a rebuild. It’s similar to react’s setState().
- deactivate() - It is called to move state from one point to another in the state tree.
- dispose() - It is called to destroy the state object.