dependOnInheritedWidgetOfExactType<U extends InheritedWidget> method

U dependOnInheritedWidgetOfExactType<U extends InheritedWidget>()

Returns the nearest parent InheritedWidget with the exact type U.

Also, if U is updated, dependenciesDidUpdate is called.

Implementation

U dependOnInheritedWidgetOfExactType<U extends InheritedWidget>() {
  final inheritedNode = parentNodes.firstWhere(
    (final parentNode) => parentNode.widget.runtimeType == U,
  ) as InheritedNode;

  late final StreamSubscription<void> subscription;

  subscription = inheritedNode.listen(() {
    subscription.cancel();
    dependenciesDidUpdate();
  });

  _dependencySubscriptions.add(subscription);

  return inheritedNode.widget as U;
}