I just started to study the bloc and I have a question. I need to change the color when the state is selected, and according to my logic, I have to do state = Colors.red
, but unfortunately this is not the case. Can you please tell me what should I do to change the status?
Here is my bloc –
class SumBloc extends Bloc<Color, SumState> {
SumBloc() : super(SumNotChosen()) {
on<Color>((event, emit) {
final state = this.state;
if(state is SumSelected) {
emit(state.color) // ??????
}
});
}
}
Here are my states –
abstract class SumState {}
class SumNotChosen extends SumState{}
class SumSelected extends SumState{
final Color color;
SumSelected({required this.color});
}
2
Answers
You can use more simpler cubit for this functionality.
When using bloc you should listen to events and change states accordingly.
sum_state.dart
sum_event.dart
sum_bloc.dart
Here yous listen to
SumSelected
event and emit new state