diff --git a/packages/flutter/lib/src/widgets/app.dart b/packages/flutter/lib/src/widgets/app.dart index 41b7579a4a..19b9681d23 100644 --- a/packages/flutter/lib/src/widgets/app.dart +++ b/packages/flutter/lib/src/widgets/app.dart @@ -361,10 +361,12 @@ class WidgetsApp extends StatefulWidget { /// also. For example, if the route was `/a/b/c`, then the app would start /// with the three routes `/a`, `/a/b`, and `/a/b/c` loaded, in that order. /// - /// If any part of this process fails to generate routes, then the - /// [initialRoute] is ignored and [Navigator.defaultRouteName] is used instead - /// (`/`). This can happen if the app is started with an intent that specifies - /// a non-existent route. + /// Intermediate routes aren't required to exist. In the example above, `/a` + /// and `/a/b` could be skipped if they have no matching route. But `/a/b/c` is + /// required to have a route, else [initialRoute] is ignored and + /// [Navigator.defaultRouteName] is used instead (`/`). This can happen if the + /// app is started with an intent that specifies a non-existent route. + /// /// The [Navigator] is only built if routes are provided (either via [home], /// [routes], [onGenerateRoute], or [onUnknownRoute]); if they are not, /// [initialRoute] must be null and [builder] must not be null. diff --git a/packages/flutter/lib/src/widgets/navigator.dart b/packages/flutter/lib/src/widgets/navigator.dart index 7c7d73cc65..3847b7235b 100644 --- a/packages/flutter/lib/src/widgets/navigator.dart +++ b/packages/flutter/lib/src/widgets/navigator.dart @@ -783,6 +783,15 @@ class Navigator extends StatefulWidget { /// then the [Navigator] would push the following routes on startup: `/`, /// `/stocks`, `/stocks/HOOLI`. This enables deep linking while allowing the /// application to maintain a predictable route history. + /// + /// If any of the intermediate routes doesn't exist, it'll simply be skipped. + /// In the example above, if `/stocks` doesn't have a corresponding route in + /// the app, it'll be skipped and only `/` and `/stocks/HOOLI` will be pushed. + /// + /// That said, the full route has to map to something in the app in order for + /// this to work. In our example, `/stocks/HOOLI` has to map to a route in the + /// app. Otherwise, [initialRoute] will be ignored and [defaultRouteName] will + /// be used instead. final String initialRoute; /// Called to generate a route for a given [RouteSettings]. @@ -1509,9 +1518,6 @@ class NavigatorState extends State with TickerProviderStateMixin { if (initialRouteName.startsWith('/') && initialRouteName.length > 1) { initialRouteName = initialRouteName.substring(1); // strip leading '/' assert(Navigator.defaultRouteName == '/'); - final List plannedInitialRouteNames = [ - Navigator.defaultRouteName, - ]; final List> plannedInitialRoutes = >[ _routeNamed(Navigator.defaultRouteName, allowNull: true, arguments: null), ]; @@ -1520,22 +1526,17 @@ class NavigatorState extends State with TickerProviderStateMixin { String routeName = ''; for (String part in routeParts) { routeName += '/$part'; - plannedInitialRouteNames.add(routeName); plannedInitialRoutes.add(_routeNamed(routeName, allowNull: true, arguments: null)); } } - if (plannedInitialRoutes.contains(null)) { + if (plannedInitialRoutes.last == null) { assert(() { FlutterError.reportError( FlutterErrorDetails( exception: 'Could not navigate to initial route.\n' 'The requested route name was: "/$initialRouteName"\n' - 'The following routes were therefore attempted:\n' - ' * ${plannedInitialRouteNames.join("\n * ")}\n' - 'This resulted in the following objects:\n' - ' * ${plannedInitialRoutes.join("\n * ")}\n' - 'One or more of those objects was null, and therefore the initial route specified will be ' + 'There was no corresponding route in the app, and therefore the initial route specified will be ' 'ignored and "${Navigator.defaultRouteName}" will be used instead.' ), ); @@ -1543,7 +1544,7 @@ class NavigatorState extends State with TickerProviderStateMixin { }()); push(_routeNamed(Navigator.defaultRouteName, arguments: null)); } else { - plannedInitialRoutes.forEach(push); + plannedInitialRoutes.where((Route route) => route != null).forEach(push); } } else { Route route; diff --git a/packages/flutter/test/widgets/navigator_test.dart b/packages/flutter/test/widgets/navigator_test.dart index 9830cb00dc..c337ac904a 100644 --- a/packages/flutter/test/widgets/navigator_test.dart +++ b/packages/flutter/test/widgets/navigator_test.dart @@ -973,4 +973,64 @@ void main() { expect(arguments.single, 'pushReplacementNamed'); arguments.clear(); }); + + testWidgets('Initial route can have gaps', (WidgetTester tester) async { + final GlobalKey keyNav = GlobalKey(); + const Key keyRoot = Key('Root'); + const Key keyA = Key('A'); + const Key keyABC = Key('ABC'); + + await tester.pumpWidget( + MaterialApp( + navigatorKey: keyNav, + initialRoute: '/A/B/C', + routes: { + '/': (BuildContext context) => Container(key: keyRoot), + '/A': (BuildContext context) => Container(key: keyA), + // The route /A/B is intentionally left out. + '/A/B/C': (BuildContext context) => Container(key: keyABC), + }, + ), + ); + + // The initial route /A/B/C should've been pushed successfully. + expect(find.byKey(keyRoot), findsOneWidget); + expect(find.byKey(keyA), findsOneWidget); + expect(find.byKey(keyABC), findsOneWidget); + + keyNav.currentState.pop(); + await tester.pumpAndSettle(); + expect(find.byKey(keyRoot), findsOneWidget); + expect(find.byKey(keyA), findsOneWidget); + expect(find.byKey(keyABC), findsNothing); + }); + + testWidgets('The full initial route has to be matched', (WidgetTester tester) async { + final GlobalKey keyNav = GlobalKey(); + const Key keyRoot = Key('Root'); + const Key keyA = Key('A'); + const Key keyAB = Key('AB'); + + await tester.pumpWidget( + MaterialApp( + navigatorKey: keyNav, + initialRoute: '/A/B/C', + routes: { + '/': (BuildContext context) => Container(key: keyRoot), + '/A': (BuildContext context) => Container(key: keyA), + '/A/B': (BuildContext context) => Container(key: keyAB), + // The route /A/B/C is intentionally left out. + }, + ), + ); + + final dynamic exception = tester.takeException(); + expect(exception is String, isTrue); + expect(exception.startsWith('Could not navigate to initial route.'), isTrue); + + // Only the root route should've been pushed. + expect(find.byKey(keyRoot), findsOneWidget); + expect(find.byKey(keyA), findsNothing); + expect(find.byKey(keyAB), findsNothing); + }); }