- override
Obtains the nearest widget of the given type, 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 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.dependenciesChanged
.
It is also possible to call this 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.dependenciesChanged
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).
Source
@override InheritedWidget inheritFromWidgetOfExactType(Type targetType) { assert(() { if (state._debugLifecycleState == _StateLifecycle.created) { throw new FlutterError( 'inheritFromWidgetOfExactType($targetType) was called before ${_state.runtimeType}.initState() completed.\n' 'When an inherited widget changes, for example if the value of Theme.of() changes, ' 'its dependent widgets are rebuilt. If the dependent widget\'s reference to ' 'the inherited widget is in a constructor or an initState() method, ' 'then the rebuilt dependent widget will not reflect the changes in the ' 'inherited widget.\n' 'Typically references to to inherited widgets should occur in widget build() methods. Alternatively, ' 'initialization based on inherited widgets can be placed in the dependenciesChanged method, which ' 'is called after initState and whenever the dependencies change thereafter.' ); } if (state._debugLifecycleState == _StateLifecycle.defunct) { throw new FlutterError( 'inheritFromWidgetOfExactType($targetType) called after dispose(): $this\n' 'This error happens if you call inheritFromWidgetOfExactType() on the ' 'BuildContext for a widget that no longer appears in the widget tree ' '(e.g., whose parent widget no longer includes the widget in its ' 'build). This error can occur when code calls ' 'inheritFromWidgetOfExactType() from a timer or an animation callback. ' 'The preferred solution is to cancel the timer or stop listening to the ' 'animation in the dispose() callback. Another solution is to check the ' '"mounted" property of this object before calling ' 'inheritFromWidgetOfExactType() to ensure the object is still in the ' 'tree.\n' 'This error might indicate a memory leak if ' 'inheritFromWidgetOfExactType() is being called because another object ' 'is retaining a reference to this State object after it has been ' 'removed from the tree. To avoid memory leaks, consider breaking the ' 'reference to this object during dispose().' ); } return true; }); return super.inheritFromWidgetOfExactType(targetType); }