dependOnInheritedWidgetOfExactType<T extends InheritedWidget> method

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

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.

Implementation

@override
T? dependOnInheritedWidgetOfExactType<T extends InheritedWidget>() {
  RenderElement? ancestor = _parent;

  var matchType = '$T';

  while (null != ancestor && ancestor.widgetRuntimeType != matchType) {
    ancestor = ancestor._parent;
  }

  if (null != ancestor && ancestor is InheritedRenderElement) {
    ancestor.addDependent(this);

    return ancestor.widget as T;
  }

  return ancestor?.widget as T?;
}