Explain the StatefulWidget lifecycle.

Updated Apr 28, 2026

Short answer

The lifecycle includes initState, didChangeDependencies, build, didUpdateWidget, and dispose.

Deep explanation

  1. createState(): Creates the State object.
  2. initState(): Called once when inserted into the tree. Used for initialization.
  3. didChangeDependencies(): Called after initState and whenever an inherited widget dependency changes.
  4. build(): Called frequently to render UI.
  5. didUpdateWidget(): Called if the parent rebuilds and provides a new widget instance to this State.
  6. 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?

More Flutter interview questions

View all →