Correctly throw when MaterialPageRoute's builder returns null (#22885)

This commit is contained in:
Mouad Debbar 2018-10-15 00:49:16 -07:00 committed by Michael Goderbauer
parent 8e2ca93f52
commit 43001a3ab6
2 changed files with 26 additions and 7 deletions

View File

@ -82,12 +82,9 @@ class MaterialPageRoute<T> extends PageRoute<T> {
}
@override
Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
final Widget result = Semantics(
scopesRoute: true,
explicitChildNodes: true,
child: builder(context),
);
Widget buildPage(BuildContext context, Animation<double> animation,
Animation<double> secondaryAnimation) {
final Widget result = builder(context);
assert(() {
if (result == null) {
throw FlutterError(
@ -97,7 +94,11 @@ class MaterialPageRoute<T> extends PageRoute<T> {
}
return true;
}());
return result;
return Semantics(
scopesRoute: true,
explicitChildNodes: true,
child: result,
);
}
@override

View File

@ -452,4 +452,22 @@ void main() {
// Page 1 is back where it started.
expect(widget1InitialTopLeft == widget1TransientTopLeft, true);
});
testWidgets('throws when builder returns null', (WidgetTester tester) async {
await tester.pumpWidget(const MaterialApp(
home: Text('Home'),
));
// No exceptions yet.
expect(tester.takeException(), isNull);
tester
.state<NavigatorState>(find.byType(Navigator))
.push(MaterialPageRoute<void>(
settings: const RouteSettings(name: 'broken'),
builder: (BuildContext context) => null,
));
await tester.pumpAndSettle();
// An exception should've been thrown because the `builder` returned null.
expect(tester.takeException(), isInstanceOf<FlutterError>());
});
}