contextOf<T extends Object?, V> static method

V contextOf<T extends Object?, V>(
  1. BuildContext context, {
  2. String? id,
  3. required Selector<T, V> selector,
})

Uses the selector callback, for determining if the widget tree of context needs to be rebuild by comparaing the previous and new result of selector, and returns it. This evaluation only occurs if one of the selected ReactterStates gets updated, or by the instance if the selector does not have any selected ReactterStates.

The selector callback has a two arguments, the first one is the instance of T type which is obtained from the closest ancestor of ReactterProvider and the second one is a Select function which allows to wrapper any ReactterStates to listen.

If T is not defined and ReactterScope is not found, will throw ReactterScopeNotFoundException.

If T is non-nullable and the instance obtained returned null, will throw ReactterInstanceNotFoundException.

If T is nullable and no matching instance is found, Selector first argument will return null.

Implementation

static V contextOf<T extends Object?, V>(
  BuildContext context, {
  String? id,
  required Selector<T, V> selector,
}) {
  final shouldFindProvider = T != getType<Object?>();
  final inheritedElement = shouldFindProvider
      ? ProvideImpl.getProviderInheritedElement<T>(context, id)
      : ReactterScope._getScopeInheritedElement(context);
  final instance = inheritedElement is ProviderElement<T>
      ? inheritedElement.instance
      : null;

  final dependency = SelectDependency(
    instance: instance as T,
    computeValue: selector as dynamic,
  );

  context.dependOnInheritedElement(
    inheritedElement!,
    aspect: dependency,
  );

  return dependency.value;
}