of<T extends IStateObservable<Object?>> static method

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

Method that allows widgets to access a Bloc instance as long as their BuildContext contains a BlocScope instance.

If we want to access an instance of BlocA which was provided higher up in the widget tree we can do so via:

BlocScope.of<BlocA>(context);

Implementation

static T of<T extends IStateObservable<Object?>>(
  BuildContext context, {
  bool listen = true,
}) {
  try {
    return Scope.of<T>(
      context,
      listen: listen,
    );
  } on ScopeNotFoundException catch (e) {
    if (e.valueType != T) rethrow;
    throw FlutterError(
      '''
      BlocScope.of() called with a context that does not contain a $T.
      No ancestor could be found starting from the context that was passed to BlocScope.of<$T>().

      This can happen if the context you used comes from a widget above the BlocScope.

      The context used was: $context
      ''',
    );
  }
}