getElementForInheritedWidgetOfExactType<T extends InheritedWidget> method

  1. @override
InheritedElement getElementForInheritedWidgetOfExactType <T extends InheritedWidget>()
inherited

Obtains the element corresponding to the nearest widget of the given type T, which must be the type of a concrete InheritedWidget subclass.

Returns null if no such element is found.

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

This method does not establish a relationship with the target in the way that dependOnInheritedWidgetOfExactType does.

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 by calling dependOnInheritedWidgetOfExactType in State.didChangeDependencies. It is safe to use this method from State.deactivate, which is called whenever the widget is removed from the tree.

Implementation

@override
InheritedElement getElementForInheritedWidgetOfExactType<T extends InheritedWidget>() {
  assert(_debugCheckStateIsActiveForAncestorLookup());
  final InheritedElement ancestor = _inheritedWidgets == null ? null : _inheritedWidgets[T];
  return ancestor;
}