useBloc<T extends BlocBase> function
Provides BlocBase
useBloc will find and return BlocBase
class using injector provided by BlocHookInjectionController
If BlocHookInjectionController
has no provided injectors, useBloc will look into widget tree and try find BlocBase using BlocProvider
useBloc will call automatically BlocBase.close
method. If needed, this behaviour can be changed by setting closeOnDispose
flag to false
Implementation
T useBloc<T extends BlocBase>({
List<dynamic> keys = const <dynamic>[],
bool closeOnDispose = true,
}) {
final context = useContext();
final configuredInjector = useHookedBlocConfig().injector;
final injector =
configuredInjector?.call() ?? BlocDefaults.defaultBlocInjector(context);
final bloc = useMemoized(() => injector<T>(), keys);
useEffect(
() {
return closeOnDispose ? bloc.close : null;
},
[bloc],
);
return bloc;
}