of<BlocType extends Bloc> static method

BlocType of<BlocType extends Bloc>(
  1. BuildContext context
)

Return the BlocType of the closest ancestor _Inherited.

If closest ancestor _Inherited is not found, ArgumentError will be thrown. To suppress the error, you should use maybeOf.

Simple usage

@override
Widget build(BuildContext context) {
  final bloc = BlocProvider.of<CounterBloc>(context);
  // ...
}

You can also define your own of method for convenience:

// Define of method at subclass.
class CounterBlocProvider extends BlocProvider<CounterBloc> {
  // ...
  static CounterBloc of(BuildContext context) => BlocProvider.of(context);
}

// Call defined [of].
final bloc = CounterBloc.of(context);

Implementation

static BlocType of<BlocType extends Bloc>(BuildContext context) =>
    _Inherited.of<BlocType>(context);