of<B extends Bloc> static method

B of<B extends Bloc>(
  1. BuildContext context, {
  2. bool listen = false,
})

Static function that returns Bloc of type B.

If listen defines as true, each time when Bloc object changes, this context is rebuilt. Custom Blocs comparison rules could be defined in BlocProvider.shouldNotify function.

Implementation

static B of<B extends Bloc>(BuildContext context, {bool listen = false}) {
  final Provider<B>? provider = listen
      ? context.dependOnInheritedWidgetOfExactType<Provider<B>>()
      : context.getElementForInheritedWidgetOfExactType<Provider<B>>()?.widget
          as Provider<B>;
  return provider?.bloc ?? (throw ProviderNotFoundError());
}