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

This commit is contained in:
Albertus Angga Raharja
2019-10-22 14:24:29 -07:00
committed by GitHub
parent a35c874f7e
commit 6c04a2a1bd
2 changed files with 35 additions and 6 deletions

View File

@@ -253,17 +253,18 @@ class CupertinoPageRoute<T> extends PageRoute<T> {
@override
Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
final Widget child = builder(context);
final Widget result = Semantics(
scopesRoute: true,
explicitChildNodes: true,
child: builder(context),
child: child,
);
assert(() {
if (result == null) {
throw FlutterError(
'The builder for route "${settings.name}" returned null.\n'
'Route builders must never return null.'
);
if (child == null) {
throw FlutterError.fromParts(<DiagnosticsNode>[
ErrorSummary('The builder for route "${settings.name}" returned null.'),
ErrorDescription('Route builders must never return null.'),
]);
}
return true;
}());

View File

@@ -15,6 +15,34 @@ void main() {
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 {
await tester.pumpWidget(
const CupertinoApp(