get<R extends Bloc> static method
Called whenever an application needs an already added Bloc
The blocs which are once instantiated, can also be cached using the useCache
option
The bloc type T
and the provided arg are both matched to return the cached bloc
Implementation
static R get<R extends Bloc>({
BuildContext? context,
arg,
bool useCache = true,
}) {
assert(arg == null || isValueType(arg),
'${arg.runtimeType} does not implement equality ');
if (!_blocs.containsKey(R)) {
throw ArgumentError(
'$R was not found in this container. Did you forgot to add() it',
);
}
if (!useCache) {
return _blocs[R]!(context, arg);
}
final cacheKey = TypeAndArg(R, arg);
return _cache.putIfAbsent(cacheKey, () => _blocs[R]!(context, arg)) as R;
}