side_effect_bloc 1.0.0 copy "side_effect_bloc: ^1.0.0" to clipboard
side_effect_bloc: ^1.0.0 copied to clipboard

Extended bloc with second stream of side effect value.

Pub

Extended bloc with a separate stream for events that should be consumed only once.

A separate thread allows you to separate events related to navigation a toast/snackbar message, for example, from the state of the bloc.

Usage #

Declare bloc #

Adding mixin to existing bloc

class FeatureBloc extends Bloc<FeatureEvent, FeatureState>
    with SideEffectBlocMixin<FeatureEvent, FeatureState, FeatureSideEffect> {
  FeatureBloc() : super(FeatureState.initial());
}

Inherit from bloc

class FeatureBloc extends SideEffectBloc<FeatureEvent, FeatureState, FeatureSideEffect>{
  FeatureBloc() : super(FeatureState.initial());
}

Emit side effect #

class FeatureBloc extends SideEffectBloc<FeatureEvent, FeatureState, FeatureSideEffect>{
  FeatureBloc() : super(FeatureState.initial()){        
    on<ItemClick>(
      (event, emit) {
        produceSideEffect(FeatureSideEffect.openItem(event.id));
      },
    );
  }
}

Listen side effect #

// feature_view.dart
BlocSideEffectListener<FeatureBloc, FeatureSideEffect>(
    listener: (BuildContext context, FeatureSideEffect sideEffect) {
        sideEffect.when(
            goToNext: () => Navigator.of(context).pushNamed("/sub"),
            openSnackbar: () {
                const snackBar = SnackBar(
                    content: Text('Yay! A SnackBar!'),
                );
                ScaffoldMessenger.of(context).showSnackBar(snackBar);
            });
    },
    child: ...
)
6
likes
150
points
609
downloads

Publisher

verified publishergeorgeci.com

Weekly Downloads

Extended bloc with second stream of side effect value.

Repository (GitHub)
View/report issues

Documentation

Documentation
API reference

License

MIT (license)

Dependencies

bloc, flutter, flutter_bloc, provider

More

Packages that depend on side_effect_bloc