Bloc Pattern In Flutter || Part 3💙

Manage the blocks of your app with BLoC!

@override
Stream<CounterState> mapEventToState(CounterEvent event) async* {
if (event is InitialCounterEvent) {
yield CounterDataState(counter: 0);
} else if (event is IncrementCounterEvent) {
yield CounterDataState(counter: event.counter + 1);
} else if (event is DecrementCounterEvent) {
yield CounterDataState(counter: (event.counter - 1).toUnsigned(event.counter.bitLength));
} else if (event is ResetCounterEvent) {
yield CounterDataState(counter: 0);
}
}
void on<E extends CounterEvent>(FutureOr<void> Function(E, Emitter<CounterState>) handler, {Stream<CounterEvent> Function(Stream<CounterEvent>, Stream<CounterEvent> Function(CounterEvent))? transformer})
CounterBloc() : super(CounterDataState(counter: 0)) {
on<InitialCounterEvent>((event, emit) => emit(CounterDataState(counter: 0)));
on<IncrementCounterEvent>((event, emit) => emit(CounterDataState(counter: event.counter + 1)));
on<DecrementCounterEvent>(
(event, emit) => emit(CounterDataState(counter: (event.counter - 1).toUnsigned(event.counter.bitLength))));
on<ResetCounterEvent>((event, emit) => emit(CounterDataState(counter: 0)));
}

Don’t forget to connect with me on:

Don’t stop, until you are breathing!💙
- Abhishek Doshi

Google Developer Expert — Dart & Flutter 💙