Unverified Commit 6c04a2a1 authored by Albertus Angga Raharja's avatar Albertus Angga Raharja Committed by GitHub

Properly throw error when CupertinoPageRoute builder returns null (#42602)

parent a35c874f
...@@ -253,17 +253,18 @@ class CupertinoPageRoute<T> extends PageRoute<T> { ...@@ -253,17 +253,18 @@ class CupertinoPageRoute<T> extends PageRoute<T> {
@override @override
Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) { Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
final Widget child = builder(context);
final Widget result = Semantics( final Widget result = Semantics(
scopesRoute: true, scopesRoute: true,
explicitChildNodes: true, explicitChildNodes: true,
child: builder(context), child: child,
); );
assert(() { assert(() {
if (result == null) { if (child == null) {
throw FlutterError( throw FlutterError.fromParts(<DiagnosticsNode>[
'The builder for route "${settings.name}" returned null.\n' ErrorSummary('The builder for route "${settings.name}" returned null.'),
'Route builders must never return null.' ErrorDescription('Route builders must never return null.'),
); ]);
} }
return true; return true;
}()); }());
......
...@@ -15,6 +15,34 @@ void main() { ...@@ -15,6 +15,34 @@ void main() {
navigatorObserver = MockNavigatorObserver(); navigatorObserver = MockNavigatorObserver();
}); });
testWidgets(
'Throws FlutterError with correct message when route builder returns null',
(WidgetTester tester) async {
await tester.pumpWidget(
const CupertinoApp(
home: Placeholder(),
),
);
tester.state<NavigatorState>(find.byType(Navigator)).push(
CupertinoPageRoute<void>(
title: 'Route 1',
builder: (_) => null,
),
);
await tester.pump();
await tester.pump(const Duration(milliseconds: 500));
final dynamic error = tester.takeException();
expect(error, isFlutterError);
expect(error.toStringDeep(), equalsIgnoringHashCodes(
'FlutterError\n'
' The builder for route "null" returned null.\n'
' Route builders must never return null.\n'
));
});
testWidgets('Middle auto-populates with title', (WidgetTester tester) async { testWidgets('Middle auto-populates with title', (WidgetTester tester) async {
await tester.pumpWidget( await tester.pumpWidget(
const CupertinoApp( const CupertinoApp(
......
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