diff --git a/packages/flutter/lib/src/cupertino/route.dart b/packages/flutter/lib/src/cupertino/route.dart index b21280514a..b7038f0401 100644 --- a/packages/flutter/lib/src/cupertino/route.dart +++ b/packages/flutter/lib/src/cupertino/route.dart @@ -253,17 +253,18 @@ class CupertinoPageRoute extends PageRoute { @override Widget buildPage(BuildContext context, Animation animation, Animation 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([ + ErrorSummary('The builder for route "${settings.name}" returned null.'), + ErrorDescription('Route builders must never return null.'), + ]); } return true; }()); diff --git a/packages/flutter/test/cupertino/route_test.dart b/packages/flutter/test/cupertino/route_test.dart index 5695150833..cb02e9b750 100644 --- a/packages/flutter/test/cupertino/route_test.dart +++ b/packages/flutter/test/cupertino/route_test.dart @@ -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(find.byType(Navigator)).push( + CupertinoPageRoute( + 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(