Skip to content

Commit dbc9306

Browse files
authored
Failure to construct ErrorWidget for build errors does not destroy tree (#117090)
1 parent 7b19b4d commit dbc9306

File tree

2 files changed

+101
-6
lines changed

2 files changed

+101
-6
lines changed

packages/flutter/lib/src/widgets/framework.dart

+9-6
Original file line numberDiff line numberDiff line change
@@ -4711,12 +4711,15 @@ abstract class Element extends DiagnosticableTree implements BuildContext {
47114711
owner!._debugCurrentBuildTarget = this;
47124712
return true;
47134713
}());
4714-
performRebuild();
4715-
assert(() {
4716-
assert(owner!._debugCurrentBuildTarget == this);
4717-
owner!._debugCurrentBuildTarget = debugPreviousBuildTarget;
4718-
return true;
4719-
}());
4714+
try {
4715+
performRebuild();
4716+
} finally {
4717+
assert(() {
4718+
assert(owner!._debugCurrentBuildTarget == this);
4719+
owner!._debugCurrentBuildTarget = debugPreviousBuildTarget;
4720+
return true;
4721+
}());
4722+
}
47204723
assert(!_dirty);
47214724
}
47224725

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/foundation.dart';
6+
import 'package:flutter/material.dart';
7+
import 'package:flutter_test/flutter_test.dart';
8+
9+
void main() {
10+
testWidgets('ErrorWidget displays actual error when throwing during build', (WidgetTester tester) async {
11+
final Key container = UniqueKey();
12+
const String errorText = 'Oh no, there was a crash!!1';
13+
14+
await tester.pumpWidget(
15+
Container(
16+
key: container,
17+
color: Colors.red,
18+
padding: const EdgeInsets.all(10),
19+
child: Builder(
20+
builder: (BuildContext context) {
21+
throw UnsupportedError(errorText);
22+
},
23+
),
24+
),
25+
);
26+
27+
expect(
28+
tester.takeException(),
29+
isA<UnsupportedError>().having((UnsupportedError error) => error.message, 'message', contains(errorText)),
30+
);
31+
32+
final ErrorWidget errorWidget = tester.widget(find.byType(ErrorWidget));
33+
expect(errorWidget.message, contains(errorText));
34+
35+
// Failure in one widget shouldn't ripple through the entire tree and effect
36+
// ancestors. Those should still be in the tree.
37+
expect(find.byKey(container), findsOneWidget);
38+
});
39+
40+
testWidgets('when constructing an ErrorWidget due to a build failure throws an error, fail gracefully', (WidgetTester tester) async {
41+
final Key container = UniqueKey();
42+
await tester.pumpWidget(
43+
Container(
44+
key: container,
45+
color: Colors.red,
46+
padding: const EdgeInsets.all(10),
47+
// This widget throws during build, which causes the construction of an
48+
// ErrorWidget with the build error. However, during construction of
49+
// that ErrorWidget, another error is thrown.
50+
child: const MyDoubleThrowingWidget(),
51+
),
52+
);
53+
54+
expect(
55+
tester.takeException(),
56+
isA<UnsupportedError>().having((UnsupportedError error) => error.message, 'message', contains(MyThrowingElement.debugFillPropertiesErrorMessage)),
57+
);
58+
59+
final ErrorWidget errorWidget = tester.widget(find.byType(ErrorWidget));
60+
expect(errorWidget.message, contains(MyThrowingElement.debugFillPropertiesErrorMessage));
61+
62+
// Failure in one widget shouldn't ripple through the entire tree and effect
63+
// ancestors. Those should still be in the tree.
64+
expect(find.byKey(container), findsOneWidget);
65+
});
66+
}
67+
68+
// This widget throws during its regular build and then again when the
69+
// ErrorWidget is constructed, which calls MyThrowingElement.debugFillProperties.
70+
class MyDoubleThrowingWidget extends StatelessWidget {
71+
const MyDoubleThrowingWidget({super.key});
72+
73+
@override
74+
StatelessElement createElement() => MyThrowingElement(this);
75+
76+
@override
77+
Widget build(BuildContext context) {
78+
throw UnsupportedError('You cannot build me!');
79+
}
80+
}
81+
82+
class MyThrowingElement extends StatelessElement {
83+
MyThrowingElement(super.widget);
84+
85+
static const String debugFillPropertiesErrorMessage = 'Crash during debugFillProperties';
86+
87+
@override
88+
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
89+
super.debugFillProperties(properties);
90+
throw UnsupportedError(debugFillPropertiesErrorMessage);
91+
}
92+
}

0 commit comments

Comments
 (0)