diff --git a/packages/flutter/lib/src/widgets/router.dart b/packages/flutter/lib/src/widgets/router.dart index 61c575fd6be..c6cf725307a 100644 --- a/packages/flutter/lib/src/widgets/router.dart +++ b/packages/flutter/lib/src/widgets/router.dart @@ -245,7 +245,12 @@ class Router extends StatefulWidget { this.routeInformationParser, required this.routerDelegate, this.backButtonDispatcher, - }) : assert(routeInformationProvider == null || routeInformationParser != null), + }) : assert( + (routeInformationProvider == null) == (routeInformationParser == null), + 'You must provide both routeInformationProvider and routeInformationParser ' + 'if this router parses route information. Otheriwse, they should both ' + 'be null.' + ), assert(routerDelegate != null), super(key: key); diff --git a/packages/flutter/test/widgets/router_test.dart b/packages/flutter/test/widgets/router_test.dart index 9725ad8050a..a3c64def01e 100644 --- a/packages/flutter/test/widgets/router_test.dart +++ b/packages/flutter/test/widgets/router_test.dart @@ -118,6 +118,50 @@ void main() { expect(find.text('popped'), findsOneWidget); }); + testWidgets('Router throw when passes only routeInformationProvider', (WidgetTester tester) async { + final SimpleRouteInformationProvider provider = SimpleRouteInformationProvider(); + provider.value = const RouteInformation( + location: 'initial', + ); + try { + Router( + routeInformationProvider: provider, + routerDelegate: SimpleRouterDelegate( + builder: (BuildContext context, RouteInformation information) { + return Text(information.location); + }, + ), + ); + } on AssertionError catch(e) { + expect( + e.message, + 'You must provide both routeInformationProvider and ' + 'routeInformationParser if this router parses route information. ' + 'Otheriwse, they should both be null.' + ); + } + }); + + testWidgets('Router throw when passes only routeInformationParser', (WidgetTester tester) async { + try { + Router( + routeInformationParser: SimpleRouteInformationParser(), + routerDelegate: SimpleRouterDelegate( + builder: (BuildContext context, RouteInformation information) { + return Text(information.location); + }, + ), + ); + } on AssertionError catch(e) { + expect( + e.message, + 'You must provide both routeInformationProvider and ' + 'routeInformationParser if this router parses route information. ' + 'Otheriwse, they should both be null.' + ); + } + }); + testWidgets('PopNavigatorRouterDelegateMixin works', (WidgetTester tester) async { final SimpleRouteInformationProvider provider = SimpleRouteInformationProvider(); provider.value = const RouteInformation(