State<T extends StatefulComponent> class abstract

The logic and internal state for a StatefulComponent.

State is information that (1) can be read synchronously when the component is built and (2) might change during the lifetime of the component. It is the responsibility of the component 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 StatefulComponent.createState method when inflating a StatefulComponent to insert it into the tree. Because a given StatefulComponent instance can be inflated multiple times (e.g., the component is incorporated into the tree in multiple places at once), there might be more than one State object associated with a given StatefulComponent instance. Similarly, if a StatefulComponent is removed from the tree and later inserted in to the tree again, the framework will call StatefulComponent.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 StatefulComponent.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 component, which are available as the context and component properties, respectively, when the initState method is called.
  • The framework calls didChangeDependencies. Subclasses of State should override didChangeDependencies to perform initialization involving InheritedComponents. If BuildContext.dependOnInheritedComponentOfExactType is called, the didChangeDependencies method will be called again if the inherited components subsequently change or if the component 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 spontaneously 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 component might rebuild and request that this location in the tree update to display a new component with the same runtimeType and Component.key. When this happens, the framework will update the component property to refer to the new component and then call the didUpdateComponent method with the previous component as an argument. State objects should override didUpdateComponent to respond to changes in their associated component (e.g., to start implicit animations). The framework always calls build after calling didUpdateComponent, which means any calls to setState in didUpdateComponent are redundant.
  • During development, if a hot reload occurs (whether initiated from the command line flutter tool by pressing r, or from an IDE), the reassemble method is called. This provides an opportunity to reinitialize any data that was prepared in the initState method.
  • If the subtree containing the State object is removed from the tree (e.g., because the parent built a component with a different runtimeType or Component.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.
  • 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 build phase 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 build phase, the framework will call dispose, which indicates that this State object will never build again. Subclasses should override this method to release any resources retained by this object.
  • 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:

  • StatefulComponent, where the current configuration of a State is hosted, and whose documentation has sample code for State.
  • StatelessComponent, for components that always build the same way given a particular configuration and ambient state.
  • InheritedComponent, for components that introduce ambient state that can be read by descendant components.
  • Component, for an overview of components in general.
Annotations

Constructors

State()

Properties

component → T
The current configuration.
no setter
context BuildContext
The location in the tree where this component builds.
no setter
hashCode int
The hash code for this object.
no setterinherited
mounted bool
Whether this State object is currently in a tree.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

activate() → void
Called when this object is reinserted into the tree after having been removed via deactivate.
build(BuildContext context) Iterable<Component>
Describes the part of the user interface represented by this component.
deactivate() → void
Called when this object is removed from the tree.
didChangeDependencies() → void
Called when a dependency of this State object changes.
didUpdateComponent(covariant T oldComponent) → void
Called whenever the component configuration changes.
dispose() → void
Called when this object is removed from the tree permanently.
initState() → void
Called when this object is inserted into the tree.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
setState(VoidCallback fn) → void
Notify the framework that the internal state of this object has changed.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited