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> { ...@@ -82,12 +82,9 @@ class MaterialPageRoute<T> extends PageRoute<T> {
} }
@override @override
Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) { Widget buildPage(BuildContext context, Animation<double> animation,
final Widget result = Semantics( Animation<double> secondaryAnimation) {
scopesRoute: true, final Widget result = builder(context);
explicitChildNodes: true,
child: builder(context),
);
assert(() { assert(() {
if (result == null) { if (result == null) {
throw FlutterError( throw FlutterError(
...@@ -97,7 +94,11 @@ class MaterialPageRoute<T> extends PageRoute<T> { ...@@ -97,7 +94,11 @@ class MaterialPageRoute<T> extends PageRoute<T> {
} }
return true; return true;
}()); }());
return result; return Semantics(
scopesRoute: true,
explicitChildNodes: true,
child: result,
);
} }
@override @override
......
...@@ -452,4 +452,22 @@ void main() { ...@@ -452,4 +452,22 @@ void main() {
// Page 1 is back where it started. // Page 1 is back where it started.
expect(widget1InitialTopLeft == widget1TransientTopLeft, true); 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