Life Cycle of Flutter
The life cycle of a Flutter widget describes how widgets are created, updated, and destroyed
while the app is running. There are two types of widgets in Flutter:
1. StatelessWidget – immutable and does not have a lifecycle beyond the build method.
2. StatefulWidget – maintains state using a State object and has a complete lifecycle.
StatefulWidget Life Cycle
1. createState()
Called once when the StatefulWidget is inserted into the widget tree. Creates the State
object for the widget.
2. initState()
Called once when the State object is first created. Used for initializations, such as setting up
variables, listeners, or fetching initial data. Always call super.initState().
3. didChangeDependencies()
Called right after initState(), and whenever the widget’s dependencies change (e.g.,
inherited widget updates). Useful when relying on Theme.of(context) or MediaQuery.
4. build()
Called multiple times whenever setState() is called, or dependencies change. Describes how
to display the widget.
5. didUpdateWidget()
Called when the parent widget rebuilds and passes new configuration to this widget. Useful
to compare old vs new widget values.
6. setState()
Triggers a rebuild by calling build() again with updated data. Only the affected widget
branch is rebuilt.
7. deactivate()
Called when the widget is removed from the tree temporarily (e.g., during navigation). It
may be reinserted later.
8. dispose()
Called when the widget is permanently removed from the widget tree. Used to clean up
resources such as closing streams, controllers, or listeners.
Flow Order
createState() → initState() → didChangeDependencies() → build()
↳ setState() → build() (repeats as needed)
↳ didUpdateWidget() → build()
deactivate() → dispose()
Summary
• StatelessWidget → Only build() method.
• StatefulWidget → Full lifecycle with initState, build, setState, and dispose.