Skip to content

An extension to the bloc state management library which adds support for firing outgoing events from blocs to external listeners.

License

Notifications You must be signed in to change notification settings

jovazcode/evented_bloc

Repository files navigation

Evented Bloc

Evented Bloc

build coverage pub package style: very good analysis License: MIT

An extension to the bloc state management library which adds support for firing outgoing events from blocs to external listeners.

This package is built to work with:

Quick Start 🚀

// The event triggered by my BLoC component.
@immutable
class CounterEvent {
  const CounterEvent.incremented() : message = 'Incremented';
  const CounterEvent.decremented() : message = 'Decremented';

  final String message;
}

// Extend `EventedCubit` instead of `Cubit`, or extend standard `Cubit`
// with `EventedMixin<Event, State>`.
// The package also exports: `EventedBloc`
class CounterCubit extends EventedCubit<CounterEvent, int> {
  CounterCubit() : super(0);

  void increment() {
    emit(state + 1);
    // Fire 'IncrementedEvent' through the event stream
    fireEvent(const CounterEvent.incremented());
  }

  void decrement() {
    emit(state - 1);
    // Fire 'DecrementedEvent' through the event stream
    fireEvent(const CounterEvent.decremented());
  }
}

void main() async {
  // Create an instance of the cubit.
  final cubit = CounterCubit();

  // Subscribe the event stream channel.
  final subscription = cubit.eventStream.listen((event) {
    print(event); // 'Incremented'!!
  });

  // Increment
  cubit.increment();
  await Future.delayed(Duration.zero);

  // Dispose
  await subscription.cancel();
  await cubit.close();
}

About

An extension to the bloc state management library which adds support for firing outgoing events from blocs to external listeners.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages