use FlutterError in MultiChildRenderObjectWidget (#37187)

This commit is contained in:
LongCatIsLooong 2019-07-29 22:11:51 -07:00 committed by GitHub
parent a0b69f3070
commit 9357e70dfd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 2 deletions

View File

@ -1688,8 +1688,9 @@ abstract class MultiChildRenderObjectWidget extends RenderObjectWidget {
assert(() {
final int index = children.indexOf(null);
if (index >= 0) {
throw AssertionError(
"The widget's children must not contain any null values, but a null value was found at index $index"
throw FlutterError(
"$runtimeType's children must not contain any null values, "
'but a null value was found at index $index'
);
}
return true;

View File

@ -31,6 +31,13 @@ void checkTree(WidgetTester tester, List<BoxDecoration> expectedDecorations) {
}
}
class MockMultiChildRenderObjectWidget extends MultiChildRenderObjectWidget {
MockMultiChildRenderObjectWidget({ Key key, List<Widget> children }) : super(key: key, children: children);
@override
RenderObject createRenderObject(BuildContext context) => null;
}
void main() {
testWidgets('MultiChildRenderObjectElement control test', (WidgetTester tester) async {
@ -345,4 +352,17 @@ void main() {
checkTree(tester, <BoxDecoration>[kBoxDecorationB, kBoxDecorationC]);
});
// Regression test for https://github.com/flutter/flutter/issues/37136.
test('provides useful assertion message when one of the children is null', () {
bool assertionTriggered = false;
try {
MockMultiChildRenderObjectWidget(children: const <Widget>[null]);
} catch (e) {
expect(e.toString(), contains("MockMultiChildRenderObjectWidget's children must not contain any null values,"));
assertionTriggered = true;
}
expect(assertionTriggered, isTrue);
});
}