See https://github.com/felangel/bloc/blob/master/examples/flutter_weather/lib/weather/cubit/weather_cubit.dart#L37-L39
is it a safe way? I think no , Because the bug reported like Crashlytics cannot catch errors
What is the best way?
You may want to do that when you want to handle the specific exception/error and show a customized message for it. But if you want to handle and report any error that could happen in any bloc to Crashlytics, you can use BlocObserver and override onError method.
onError will never called
Yeah, you are right. And that is because the exception is already handled locally intentionally. The error, in this case, is not a bug, we know it could happen at some point when we are not connected to the internet (assuming we are fetching the weather from some REST API), and when it does, it may not be necessary to report as a bug. Instead, we show the user that there is an internet connection error so that they can check their internet connection or try again sometime later.
Here is the error handled on the UI side.
@Elias8 what happens if we call manually onError or rethrow , is there any difference between onError and rethrow ?
Hey @sm2017, sorry for the late response.
If you are going to report the error to Crashlytics, I don't think it is a good idea to call the onError method on BlocObserver manually since the bloc library does that for you when there is an error in the bloc. And rethrow will propagate the error to the caller so that the caller can handle the exception. Also, rethrow preserves the original stack trace of the exception.
The difference is, rethrow is a keyword from the language itself, while onError is a custom-defined method in the BlocObserver class.
For more on rethrow check out the doc.