Correctly throw when MaterialPageRoute's builder returns null (#22885)
This commit is contained in:
parent
8e2ca93f52
commit
43001a3ab6
@ -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> {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}());
|
}());
|
||||||
return result;
|
return Semantics(
|
||||||
|
scopesRoute: true,
|
||||||
|
explicitChildNodes: true,
|
||||||
|
child: result,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
@ -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>());
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user