Last updated:
0 purchases
side effect cubit
Side Effect Cubit #
An extension to the bloc state management library which serve an additional stream for events that should be consumed only once - as usual called one-time events.
Usage #
1. Adding to existing one #
For Bloc:
class FeatureBloc extends Bloc<FeatureEvent, FeatureState>
with SideEffectBlocMixin<FeatureEvent, FeatureState, FeatureSideEffect> {
FeatureBloc() : super(FeatureState.initial());
}
copied to clipboard
For Cubit:
class FeatureCubit extends Cubit<FeatureState>
with SideEffectCubitMixin<FeatureState, FeatureSideEffect> {
FeatureCubit() : super(FeatureState.initial());
}
copied to clipboard
2. Inherit #
For Bloc:
class FeatureBloc extends SideEffectBloc<FeatureEvent, FeatureState, FeatureSideEffect>{
FeatureBloc() : super(FeatureState.initial());
}
copied to clipboard
For Cubit:
class FeatureCubit extends SideEffectCubit<FeatureState, FeatureSideEffect> {
FeatureCubit() : super(FeatureState.initial());
}
copied to clipboard
Emit side effect #
class FeatureBloc extends SideEffectBloc<FeatureEvent, FeatureState, FeatureSideEffect>{
FeatureBloc() : super(FeatureState.initial()){
on<ItemClick>(
(event, emit) {
produceSideEffect(FeatureSideEffect.openItem(event.id));
},
);
}
}
copied to clipboard
class FeatureCubit extends SideEffectCubit<FeatureState, FeatureSideEffect> {
FeatureCubit() : super(FeatureState.initial());
Future<void> doSomething() {
produceSideEffect(FeatureSideEffect.openItem(event.id));
}
}
copied to clipboard
Listen side effect #
BlocSideEffectListener<FeatureBloc, FeatureSideEffect>(
listener: (BuildContext context, FeatureSideEffect sideEffect) {
sideEffect.when(
goToNextScreen: () => Navigator.of(context).pushNamed("/second_screen"),
showPopupError: (errMsg) {
// ....
});
},
child: ...
)
copied to clipboard
BlocSideEffectConsumer<FeatureBloc, FeatureState, FeatureSideEffect>(
sideEffectListener: (BuildContext context, FeatureSideEffect sideEffect) {
},
listenWhen: (previos, current) {},
listen: (previous, current) {},
buildWhen: (previous, current) => true,
builder: (BuildContext context, FeatureState state) {
return ...
}
)
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.