getInheritedWidgetOfExactType<T extends InheritedWidget> method

  1. @override
T? getInheritedWidgetOfExactType<T extends InheritedWidget>()
override

Returns the nearest widget of the given InheritedWidget subclass T or null if an appropriate ancestor is not found.

This method does not introduce a dependency the way that the more typical dependOnInheritedWidgetOfExactType does, so this context will not be rebuilt if the InheritedWidget changes. This function is meant for those uncommon use cases where a dependency is undesirable.

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, as long as that value is not cached and/or reused later.

Calling this method is O(1) with a small constant factor.

Implementation

@override
T? getInheritedWidgetOfExactType<T extends InheritedWidget>() {
  return getElementForInheritedWidgetOfExactType<T>()?.widget as T?;
}