of<B extends Bloc> static method
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());
}