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
- 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
configproperties, 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
buildmethod 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
setStatemethod, 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
key. When this happens, the framework will update the
configproperty to refer to the new widget and then call the
didUpdateConfigmethod with the previous widget as an argument. State objects should override
didUpdateConfigto respond to changes in their associated wiget (e.g., to start implicit animations). The framework always calls
didUpdateConfig, which means any calls to
- If the subtree containing the State object is removed from the tree
(e.g., because the parent built a widget with a different
key), the framework calls the
deactivatemethod. 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
buildto 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
mountedproperty is false. It is an error to call
setStateat this point. This stage of the lifecycle is terminal: there is no way to remount a State object that has been disposed.
- config → T
The current configuration.read-only
- context → BuildContext
The location in the tree where this widget builds.read-only
- mounted → bool
Stateobject is currently in a tree.read-only
- 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
BuildContext context) → Widget
Describes the part of the user interface represented by this widget.protected
) → void
Called when this object is removed from the tree.mustCallSuper, protected
List<String> description) → void
Add additional information to the given description for use by toString.mustCallSuper, protected
) → void
Called when a dependency of this State object changes.mustCallSuper, protected
T, oldConfig) → void
Called whenever the configuration changes.protected
) → void
Called when this object is removed from the tree permanently.mustCallSuper, protected
) → void
Called when this object is inserted into the tree.mustCallSuper, protected
) → void
Called whenever the application is reassembled during debugging.mustCallSuper, protected
VoidCallback fn) → void
Notify the framework that the internal state of this object has changed.protected
) → String
Returns a string representation of this object.
Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed.inherited