I have the following Bloc:
class ClockBloc extends Bloc<ClockEvent, ClockState> {
const ClockBloc() : super(const ClockState()) {
on<ClockEventAddClock>(_addClock);
}
Future<void> _addClock(
ClockEventAddClock event,
Emitter<ClockState> emit,
) async {
List<String> newItems = /* Some logic */;
bool error = /* Some logic */;
emit(ClockState(items: newItems, isError: error));
}
}
class ClockState extends Equatable {
const ClockState(this.items = const <String>[], this.isError = false,);
final List<String> items;
final bool isError;
@override
List<Object?> get props => [isError];
}
Now, in my widget, I have a BlocListener which is supposed to display a dialog box (on button press) if state.isError = true. The thing is, the bloc may return the exact same state consecutively(when you keep pressing the button) - meaning the listener won't be triggered, and the dialog is never shown again.
How can I listen to the same state and trigger a dialog as long as state.isError = true?
UI:
@override
Widget build(BuildContext context) {
return BlocListener<ClockBloc, ClockState>(
listener: (context, state) {
if (state.isError) {
/*Display dialog*/
// this will only show when isError goes from 'false' -> 'true',
// and not from 'true' -> 'true''
}
}
);
}
I understand that since I'm using Equatable, the new state I'm emitting is compared against the previous one, and since they're identical, the new state is never emitted. So what approach would you choose? I thought about changing to some default state right before I emit the data back, but I'm sure there's a better solution.