Describe the bug
In my app I use a bloc to fetch data from a repository; the repository returns a Future. In mapEventToState method I'm using a try/catch to yield ResultSuccess in case of success or ResultError in case of an exception is caused by the repository code.
The problem is that whenever an exception is thrown the ResultError state is not yield.
I've used the good old debug method of print and I can say that the code inside catch is executed up until the yield because the print statement is executed.
To Reproduce
This is part of my source code:
ResultBloc._(MeasurementDatabase db): _repository = ResultRepository((db));
factory ResultBloc.create(MeasurementDatabase db, int resultId) =>
ResultBloc._(db)..add(FetchResult(resultId));
@override
ResultState get initialState => ResultLoading();
@override
Stream<ResultState> mapEventToState(ResultEvents event) async* {
if (event is FetchResult) {
// fetch the data from the repository
try {
final res = await _repository.getResult(event.measurementId);
yield ResultSuccess(res);
} catch(error) {
print("Error $error");
yield ResultSuccess(error);
}
}
// ...
}
@override
Widget build(BuildContext context) {
// Get the resultId from the arguments
final Test test = ModalRoute.of(context).settings.arguments;
return Scaffold(
body: BlocProvider(
create: (context) => ResultBloc.create(
Provider.of<MeasurementDatabase>(context, listen: false),
test?.id
),
child: BlocConsumer<ResultBloc, ResultState>(
listenWhen: (_, current) => current is ResultError || current is ResultExportSuccess,
listener: (context, state) {
Scaffold.of(context).showSnackBar(SnackBar(
behavior: SnackBarBehavior.floating,
content: state is ResultExportSuccess
? Text("Export success!")
: Text("An unexpected error occurred!")
));
},
buildWhen: (previous, current) {
return !(current is ResultExportSuccess) &&
!(previous is ResultSuccess && current is ResultError);
},
builder: (context, state) {
print("ResultScreen.build: $state");
// Display the loading screen
if (state is ResultLoading)
return _loadingScreen(context, test);
// Display the data screen
else
return _successScreen(context, test, state is ResultSuccess? state: null);
},
),
),
);
}
Expected behavior
The expected behaviour is that when the error occur the bloc will yield ResultError and the view will show a snakbar and the code in _successScreen
*Logs *
[✓] Flutter (Channel master, 1.18.0-11.0.pre, on Linux, locale it_IT.UTF-8)
• Flutter version 1.18.0-11.0.pre at /home/lorenzo/flutter
• Framework revision 8568eda15b (10 giorni fa), 2020-05-05 16:10:41 -0700
• Engine revision 33d2367950
• Dart version 2.9.0 (build 2.9.0-5.0.dev 9c94f08410)
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.0-rc2)
• Android SDK at /home/lorenzo/Android/Sdk
• Platform android-29, build-tools 29.0.0-rc2
• Java binary at: /home/lorenzo/Scaricati/uncompressed-sources/android-studio/jre/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211)
• All Android licenses accepted.
[✓] Android Studio (version 3.6)
• Android Studio at /home/lorenzo/Scaricati/uncompressed-sources/android-studio
• Flutter plugin version 45.1.1
• Dart plugin version 192.7761
• Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211)
[!] IntelliJ IDEA Ultimate Edition (version 2018.1)
• IntelliJ at /home/lorenzo/Scaricati/uncompressed-sources/idea-IU-181.4668.68
✗ Flutter plugin not installed; this adds Flutter specific functionality.
✗ Dart plugin not installed; this adds Dart specific functionality.
• For information about installing plugins, see
https://flutter.dev/intellij-setup/#installing-the-plugins
[✓] IntelliJ IDEA Ultimate Edition (version 2019.3)
• IntelliJ at /home/lorenzo/Scaricati/uncompressed-sources/idea-IU-193.6494.35
• Flutter plugin version 43.0.3
• Dart plugin version 193.6494.35
[✓] Connected device (1 available)
• HUAWEI MLA L11 • GLDDU16903001578 • android-arm64 • Android 7.0 (API 24)
! Doctor found issues in 1 category.
Hi @Supercaly 👋
Thanks for opening an issue!
I'm guessing the issue you're facing is related to https://bloclibrary.dev/#/faqs?id=state-not-updating. Let me know if that helps 👍
Tanks for the quick response,
I've looked at the link and I'm not using equitable, but basically the problem is that my ResultError object want an Error parameter, but in the catch, I was giving him an Exception.
I've discovered this updating the flutter_bloc to 4.0.0 before he didn't show me any error in the log.
Changing the parameter form Error to String fixed the problem.
Most helpful comment
Tanks for the quick response,
I've looked at the link and I'm not using equitable, but basically the problem is that my
ResultErrorobject want anErrorparameter, but in the catch, I was giving him anException.I've discovered this updating the flutter_bloc to 4.0.0 before he didn't show me any error in the log.
Changing the parameter form
ErrortoStringfixed the problem.