Skip to content

Commit

Permalink
fix: If builder of Observer errors, further error LateInitializationE…
Browse files Browse the repository at this point in the history
…rror: Local 'built' has not been initialized. will happen in addition to the actual error, reducing developer experience #780
  • Loading branch information
fzyzcjy committed Mar 22, 2022
1 parent b324e5e commit e2954b3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 7 additions & 2 deletions flutter_mobx/lib/src/observer_widget_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ mixin ObserverElementMixin on ComponentElement {

@override
Widget build() {
late final Widget built;
Widget? built;

reaction.track(() {
built = super.build();
Expand All @@ -128,7 +128,12 @@ mixin ObserverElementMixin on ComponentElement {
);
}

return built;
// Better than a "LateInitializationError" which confuses the user, see #780
if (built == null) {
throw Exception('Error building widget');
}

return built!;
}

@override
Expand Down
4 changes: 2 additions & 2 deletions flutter_mobx/test/flutter_mobx_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void main() {
});

testWidgets(
'Observer should re-throw exceptions occuring inside the reaction',
'Observer should throw exception if exceptions occur inside the reaction',
(tester) async {
dynamic exception;

Expand Down Expand Up @@ -98,7 +98,7 @@ void main() {

expect(tester.firstWidget(find.byWidget(widget)), equals(widget));

expect(exception, isInstanceOf<Error>());
expect(exception.message, 'Error building widget');
});

testWidgets('Observer should report Flutter errors during invalidation',
Expand Down

0 comments on commit e2954b3

Please sign in to comment.