stream_bloc 0.5.0 stream_bloc: ^0.5.0 copied to clipboard
Modern implementation of the Original BLoC that uses asynchronous generators to describe relationships between events and states.
// ignore_for_file: avoid_print
import 'package:stream_bloc/stream_bloc.dart';
abstract class CounterEvent {}
class Increment implements CounterEvent {}
class Decrement implements CounterEvent {}
class CounterBloc extends StreamBloc<CounterEvent, int> {
CounterBloc() : super(0);
@override
Stream<int> mapEventToStates(CounterEvent event) async* {
if (event is Increment) {
yield state + 1;
} else if (event is Decrement) {
yield state - 1;
}
}
}
Future<void> main(List<String> arguments) async {
final bloc = CounterBloc();
final printSubscription = bloc.stream.listen(print);
bloc
..add(Increment())
..add(Increment())
..add(Increment())
..add(Decrement());
await Future<void>.delayed(Duration.zero);
await printSubscription.cancel();
await bloc.close();
}