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

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

Method that allows widgets to access a Bloc or Cubit instance as long as their BuildContext contains a BlocProvider instance.

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

BlocProvider.of<BlocA>(context);

Implementation

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

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

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