inheritFrom<T extends InheritedMediator> static method

T? inheritFrom<T extends InheritedMediator>(
  1. BuildContext context, {
  2. Iterable<Object>? aspect,
})

Makes context dependent on the specified aspect of an InheritedModel of type T.

When the given aspect of the model changes, the context will be rebuilt. The updateShouldNotifyDependent method must determine if a change in the model widget corresponds to an aspect value.

The dependencies created by this method target all InheritedModel ancestors of type T up to and including the first one.

If aspect is null this method is the same as context.dependOnInheritedWidgetOfExactType<T>().

If no ancestor of type T exists, null is returned.

Implementation

static T? inheritFrom<T extends InheritedMediator>(BuildContext context,
    {Iterable<Object>? aspect}) {
  if (aspect == null) return context.dependOnInheritedWidgetOfExactType<T>();

  // Create a dependency on all of the type T ancestor models up until
  // a model is found.
  final models = <InheritedElement>[];
  _findModels<T>(context, aspect, models);
  if (models.isEmpty) {
    return null;
  }

  final InheritedElement lastModel = models.last;
  for (final InheritedElement model in models) {
    final value =
        context.dependOnInheritedElement(model, aspect: aspect) as T;
    if (model == lastModel) return value;
  }

  assert(false);
  return null;
}