diff --git a/packages/flutter/lib/src/material/page.dart b/packages/flutter/lib/src/material/page.dart index 78856058c0..03908820e9 100644 --- a/packages/flutter/lib/src/material/page.dart +++ b/packages/flutter/lib/src/material/page.dart @@ -82,12 +82,9 @@ class MaterialPageRoute extends PageRoute { } @override - Widget buildPage(BuildContext context, Animation animation, Animation secondaryAnimation) { - final Widget result = Semantics( - scopesRoute: true, - explicitChildNodes: true, - child: builder(context), - ); + Widget buildPage(BuildContext context, Animation animation, + Animation secondaryAnimation) { + final Widget result = builder(context); assert(() { if (result == null) { throw FlutterError( @@ -97,7 +94,11 @@ class MaterialPageRoute extends PageRoute { } return true; }()); - return result; + return Semantics( + scopesRoute: true, + explicitChildNodes: true, + child: result, + ); } @override diff --git a/packages/flutter/test/material/page_test.dart b/packages/flutter/test/material/page_test.dart index 9540ad0cbd..9b039bfd44 100644 --- a/packages/flutter/test/material/page_test.dart +++ b/packages/flutter/test/material/page_test.dart @@ -452,4 +452,22 @@ void main() { // Page 1 is back where it started. 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(find.byType(Navigator)) + .push(MaterialPageRoute( + 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()); + }); }