Commit 43001a3a authored by Mouad Debbar's avatar Mouad Debbar Committed by Michael Goderbauer

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

parent 8e2ca93f
......@@ -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
......
......@@ -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>());
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment