of<T> static method

T of<T>(
  1. BuildContext context, {
  2. bool listen = true,
})

The value of the nearest provider of a T above context; context depends on it when listen: rebuilt each time it changes. A ProviderNotFoundException when there is none, unless T is nullable: then null.

Implementation

static T of<T>(BuildContext context, {bool listen = true}) {
  final scope = _scopeOf<T>(context);
  if (listen && scope != null) {
    context.dependOnInheritedElement(scope);
    scope._dependOnAll(context);
  }
  final value = scope?.value;
  if (value is! T) {
    throw ProviderNullException(T, context.widget.runtimeType);
  }
  return value;
}