dependOnInheritedWidgetOfExactType<T extends InheritedWidget> method
Null safety
- {Object? aspect}
Obtains the nearest widget of the given type T
, which must be the type of a
concrete InheritedWidget subclass, and registers this build context with
that widget such that when that widget changes (or a new widget of that
type is introduced, or the widget goes away), this build context is
rebuilt so that it can obtain new values from that widget.
This is typically called implicitly from of()
static methods, e.g.
Theme.of.
This method should not be called from widget constructors or from State.initState methods, because those methods would not get called again if the inherited value were to change. To ensure that the widget correctly updates itself when the inherited value changes, only call this (directly or indirectly) from build methods, layout and paint callbacks, or from State.didChangeDependencies.
This method should not be called from State.dispose because the element tree is no longer stable at that time. To refer to an ancestor from that method, save a reference to the ancestor in State.didChangeDependencies. It is safe to use this method from State.deactivate, which is called whenever the widget is removed from the tree.
It is also possible to call this method from interaction event handlers (e.g. gesture callbacks) or timers, to obtain a value once, if that value is not going to be cached and reused later.
Calling this method is O(1) with a small constant factor, but will lead to the widget being rebuilt more often.
Once a widget registers a dependency on a particular type by calling this method, it will be rebuilt, and State.didChangeDependencies will be called, whenever changes occur relating to that widget until the next time the widget or one of its ancestors is moved (for example, because an ancestor is added or removed).
The aspect
parameter is only used when T
is an
InheritedWidget subclasses that supports partial updates, like
InheritedModel. It specifies what "aspect" of the inherited
widget this context depends on.
Implementation
T? dependOnInheritedWidgetOfExactType<T extends InheritedWidget>({ Object? aspect });