Creating a [State] object and before calling [initState], the framework “mounts” the [State] object by associating it with a [BuildContext]. The [State] object remains mounted until the framework calls [dispose], after which time the framework will never ask the [State] object to [build] again.
The framework can call this method multiple times over the lifetime of a [StatefulWidget]. For example, if the widget is inserted into the tree in multiple locations, the framework will create a separate [State] object for each location. Similarly, if the widget is removed from the tree and later inserted into the tree again, the framework will call [createState] again to create a fresh [State] object, simplifying the lifecycle of [State] objects.
1) created: The [State.initState] method has been called but the [State] object is not yet ready to build. [State.didChangeDependencies] is called at this time.
2) ready: The [State] object is ready to build and [State.dispose] has not yet been called.
3) defunct: The [State.dispose] method has been called and the [State] object is no longer able to build.
The framework creates a State object by calling
- 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 [widget] properties, respectively, when the [initState] method is called.
- 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 [Widget.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.
- 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.