-
Notifications
You must be signed in to change notification settings - Fork 53
Open
Description
So using StreamController to store the count state and display it with StreamBuilder and AsyncSnapshot in the UI are really complicated for Flutter beginner without BloC experience. For me I would simply storing the count state as an int and registering events under the bloc class with on instead of mapping them, then display it with context
For example
class CounterBloc extends Bloc<CounterEvent, CounterState> {
CounterBloc() : super(const Counter(0)) {
on<CounterIncrementPressed>(
(event, emit) => emit(Counter(state.count + 1)));
on<CounterDecrementPressed>(
(event, emit) => emit(Counter(state.count - 1)));
on<CounterResetPressed>((event, emit) => emit(const Counter(0)));
}
}
Then in UI
body: Center(
child: BlocBuilder<CounterBloc, int>(
builder: (context, count) {
return Text('$count', style: textTheme.headline2);
},
),
),
Metadata
Metadata
Assignees
Labels
No labels