I see people use onError
just for debugging. I thought I could use it to emit new states like emit(ErrorState(message: "An error"))
. But with the new versions of the bloc package, we should use emitters provided to the handlers and shouldn't use the dedicated function emit
directly.
Currently I have try/catch blocks in all of my event handlers. If any error occurs, I emit the ErrorState
with a message and show it in the UI with a widget. Is this how should I handle errors? This makes event handler functions to look awful with these try/catchs. I wonder if I'm doing it correct and want to know how should it be done actually?
void _startExercise(ExerciseStarted event, Emitter<ExerciseState> emit) async {
emit(ExerciseLoadingState());
try {
final something = await _repository.doSomething();
emit(ExerciseLoadedState(something: something));
} catch (e) {
log(e.toString());
emit(const ExerciseErrorState());
}
}