useBloc<B extends BlocBase<S> , S extends Object> function
S
useBloc<B extends BlocBase<S> , S extends Object>({
- B? bloc,
- BlocHookListener<
S> ? onEmitted,
Subscribes to a Cubit and handles a listener or a rebuild.
Whenever BlocBase.state updates, it will mark the caller HookWidget
as needing build if either allowRebuild
is true
or buildWhen
invocation returns true
.
if bloc
is null, it will be inherited with context.bloc()
The following example showcase a basic counter application.
class CounterCubit extends Cubit<int> {
CounterCubit() : super(0);
void increment() => emit(state + 1);
}
class Counter extends HookWidget {
@override
Widget build(BuildContext context) {
// automatically triggers a rebuild of Counter widget
final counterCubit = useBloc<CounterCubit, int>();
return GestureDetector(
onTap: () => counterCubit.increment(),
child: Text('${counter.state}'),
);
}
}
See also:
Implementation
S useBloc<B extends BlocBase<S>, S extends Object>({
B? bloc,
BlocHookListener<S>? onEmitted,
}) {
final bloc0 = bloc ?? useContext().read<B>();
return use(_BlocHook<S>(bloc0, onEmitted));
}