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:
// 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();
}