contextOf<T extends ReactterContext?> static method

T contextOf<T extends ReactterContext?>(
  1. BuildContext context, {
  2. String? id,
  3. ListenHooks<T>? listenHooks,
  4. bool listen = true,
})

Returns a _instance of T and puts contexts listen to when it should be re-rendered

Implementation

static T contextOf<T extends ReactterContext?>(
  BuildContext context, {
  String? id,
  ListenHooks<T>? listenHooks,
  bool listen = true,
}) {
  final scopeInheritedElement = context
          .getElementForInheritedWidgetOfExactType<ReactterScopeInherited>()
      as ReactterScopeInheritedElement?;

  final providerInheritedElement =
      _getProviderInheritedElement<T>(context, id);

  if (providerInheritedElement?._instance == null && null is! T) {
    throw ReactterContextNotFoundException(T, context.widget.runtimeType);
  }

  final instance = providerInheritedElement?._instance as T;

  if (!listen || instance == null) {
    return instance;
  }

  context.dependOnInheritedElement(scopeInheritedElement!);

  if (listenHooks != null) {
    final hooks = listenHooks(instance);

    scopeInheritedElement.dependOnHooks(hooks);

    return instance;
  }

  scopeInheritedElement.dependOnInstance(instance);

  return instance;
}