Skip to content

State & Lifecycle

In React, state lives inside the component — either as a useState hook variable or as this.state in a class component. When state changes, React re-renders the component function (or calls render()).

Flutter separates the concern differently. A StatefulWidget is the immutable configuration object (like your props/JSX tag), and a companion State<T> class holds the mutable state and owns the build() method. The two work as a unit: the widget provides identity and configuration; the state provides memory and lifecycle.

Flutter’s widget tree is rebuilt very frequently — any ancestor rebuild can recreate widget objects. If state lived on the widget itself it would be destroyed on every rebuild. By keeping state in a separate, longer-lived State object that Flutter manages, the widget description can be cheaply recreated while state survives across rebuilds.

React conceptFlutter equivalent
useState / this.statefields on the State<T> class
setState(newVal)setState(() { field = newVal; })
Component function / render()State.build(BuildContext)
useEffect(fn, []) — mountinitState()
useEffect(fn, [dep]) — prop changedidUpdateWidget(oldWidget)
useEffect(() => cleanup, []) — unmountdispose()
useContext / React ContextInheritedWidget / BuildContext
React key propFlutter Key on a widget
  1. State & Lifecycle overview ← you are here
  2. setState vs useState — the mutable state API
  3. build() vs render() — Flutter’s pure rebuild model
  4. The lifecycle in depthinitState, didChangeDependencies, didUpdateWidget, dispose
  5. Rebuilds & Keysconst widgets, identity, and reconciliation
Why does Flutter separate StatefulWidget from its State class?
Which React hook maps most closely to Flutter's initState()?
Which Flutter method is the equivalent of a useEffect cleanup function?