The logic and internal state for a StatefulWidget.

State is information that (1) can be read synchronously when the widget is built and (2) might change during the lifetime of the widget. It is the responsibility of the widget implementer to ensure that the State is promptly notified when such state changes, using State.setState.

State objects are created by the framework by calling the StatefulWidget.createState method when inflating a StatefulWidget to insert it into the tree. Because a given StatefulWidget instance can be inflated multiple times (e.g., the widget is incorporated into the tree in multiple places at once), there might be more than one State object associated with a given StatefulWidget instance. Similarly, if a StatefulWidget is removed from the tree and later inserted in to the tree again, the framework will call StatefulWidget.createState again to create a fresh State object, simplifying the lifecycle of State objects.

State objects have the following lifecycle:

  • The framework creates a State object by calling StatefulWidget.createState.
  • The newly created State object is associated with a BuildContext. This association is permanent: the State object will never change its BuildContext. However, the BuildContext itself can be moved around the tree along with its subtree. At this point, the State object is considered mounted.
  • The framework calls initState. Subclasses of State should override initState to perform one-time initialization that depends on the BuildContext or the widget, which are available as the context and config properties, respectively, when the initState method is called.
  • The framework calls dependenciesChanged. Subclasses of State should override dependenciesChanged to perform initialization involving InheritedWidgets. If inheritFromWidgetOfExactType is called, the dependenciesChanged method will be called again if the inherited widgets subsequently change or if the widget moves in the tree.
  • At this point, the State object is fully initialized and the framework might call its build method any number of times to obtain a description of the user interface for this subtree. State objects can spontanteously request to rebuild their subtree by callings their setState method, which indicates that some of their internal state has changed in a way that might impact the user interface in this subtree.
  • During this time, a parent widget might rebuild and request that this location in the tree update to display a new widget with the same runtimeType and key. When this happens, the framework will update the config property to refer to the new widget and then call the didUpdateConfig method with the previous widget as an argument. State objects should override didUpdateConfig to respond to changes in their associated wiget (e.g., to start implicit animations). The framework always calls build after calling didUpdateConfig, which means any calls to setState in didUpdateConfig are redundant.
  • If the subtree containing the State object is removed from the tree (e.g., because the parent built a widget with a different runtimeType or key), the framework calls the deactivate method. Subclasses should override this method to clean up any links between this object and other elements in the tree (e.g. if you have provided an ancestor with a pointer to a descendant's RenderObject).
  • At this point, the framework might reinsert this subtree into another part of the tree. If that happens, the framework will ensure that it calls build to give the State object a chance to adapt to its new location in the tree. If the framework does reinsert this subtree, it will do so before the end of the animation frame in which the subtree was removed from the tree. For this reason, State objects can defer releasing most resources until the framework calls their dispose method.
  • If the framework does not reinsert this subtree by the end of the current animation frame, the framework will call dispose, which indiciates that this State object will never build again. Subclasses should override this method to release any resources retained by this object (e.g., stop any active animations).
  • After the framework calls dispose, the State object is considered unmounted and the mounted property is false. It is an error to call setState at this point. This stage of the lifecycle is terminal: there is no way to remount a State object that has been disposed.

See also:

  • StatefulWidget, where the current configuration of a State is hosted (see config).
  • StatelessWidget, for widgets that always build the same way given a particular configuration and ambient state.
  • InheritedWidget, for widgets that introduce ambient state that can be read by descendant widgets.
  • optionalTypeArgs




config → T

The current configuration.

context BuildContext

The location in the tree where this widget builds.

mounted bool

Whether this State object is currently in a tree.

hashCode int

Get a hash code for this object.

read-only, inherited
runtimeType Type

A representation of the runtime type of the object.

read-only, inherited


operator ==(other) bool

The equality operator.



build(BuildContext context) Widget

Describes the part of the user interface represented by this widget.

deactivate() → void

Called when this object is removed from the tree.

mustCallSuper, protected
debugFillDescription(List<String> description) → void

Add additional information to the given description for use by toString.

mustCallSuper, protected
dependenciesChanged() → void

Called when a dependency of this State object changes.

mustCallSuper, protected
didUpdateConfig(T, oldConfig) → void

Called whenever the configuration changes.

dispose() → void

Called when this object is removed from the tree permanently.

mustCallSuper, protected
initState() → void

Called when this object is inserted into the tree.

mustCallSuper, protected
reassemble() → void

Called whenever the application is reassembled during debugging.

mustCallSuper, protected
setState(VoidCallback fn) → void

Notify the framework that the internal state of this object has changed.

toString() String

Returns a string representation of this object.

noSuchMethod(Invocation invocation) → dynamic

Invoked when a non-existent method or property is accessed.