midFlutter
Explain the StatefulWidget lifecycle.
Updated Apr 28, 2026
Short answer
The lifecycle includes initState, didChangeDependencies, build, didUpdateWidget, and dispose.
Deep explanation
createState(): Creates the State object.initState(): Called once when inserted into the tree. Used for initialization.didChangeDependencies(): Called after initState and whenever an inherited widget dependency changes.build(): Called frequently to render UI.didUpdateWidget(): Called if the parent rebuilds and provides a new widget instance to this State.dispose(): Called when the widget is permanently removed from the tree. Used for cleanup.
Real-world example
Initializing an AnimationController in initState and cleaning it up in dispose to prevent memory leaks.
Common mistakes
- Calling `setState` inside `initState()`, which is redundant, or performing heavy async work directly in `dispose()`.
Follow-up questions
- Can you use BuildContext in initState?
- What triggers didUpdateWidget?