diff --git a/packages/flutter/lib/src/widgets/router.dart b/packages/flutter/lib/src/widgets/router.dart index 918d36b526..63db13e365 100644 --- a/packages/flutter/lib/src/widgets/router.dart +++ b/packages/flutter/lib/src/widgets/router.dart @@ -5,6 +5,7 @@ import 'dart:async'; import 'dart:collection'; +import 'package:collection/collection.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/scheduler.dart'; import 'package:flutter/services.dart'; @@ -1466,12 +1467,18 @@ class PlatformRouteInformationProvider extends RouteInformationProvider with Wid required RouteInformation initialRouteInformation, }) : _value = initialRouteInformation; + static bool _equals(Uri a, Uri b) { + return a.path == b.path + && a.fragment == b.fragment + && const DeepCollectionEquality.unordered().equals(a.queryParametersAll, b.queryParametersAll); + } + @override void routerReportsNewRouteInformation(RouteInformation routeInformation, {RouteInformationReportingType type = RouteInformationReportingType.none}) { final bool replace = type == RouteInformationReportingType.neglect || (type == RouteInformationReportingType.none && - _valueInEngine.uri == routeInformation.uri); + _equals(_valueInEngine.uri, routeInformation.uri)); SystemNavigator.selectMultiEntryHistory(); SystemNavigator.routeInformationUpdated( uri: routeInformation.uri, diff --git a/packages/flutter/test/widgets/router_test.dart b/packages/flutter/test/widgets/router_test.dart index f07d59245b..cf46e0fc3d 100644 --- a/packages/flutter/test/widgets/router_test.dart +++ b/packages/flutter/test/widgets/router_test.dart @@ -867,6 +867,76 @@ testWidgets('ChildBackButtonDispatcher take priority recursively', (WidgetTester ]); }); + testWidgets('PlatformRouteInformationProvider does not push new entry if query parameters are semantically the same', (WidgetTester tester) async { + final List log = []; + TestDefaultBinaryMessengerBinding + .instance.defaultBinaryMessenger + .setMockMethodCallHandler( + SystemChannels.navigation, + (MethodCall methodCall) async { + log.add(methodCall); + return null; + } + ); + final RouteInformation initial = RouteInformation( + uri: Uri.parse('initial?a=ws/abcd'), + ); + final RouteInformationProvider provider = PlatformRouteInformationProvider( + initialRouteInformation: initial + ); + // Make sure engine is updated with initial route + provider.routerReportsNewRouteInformation(initial); + log.clear(); + + provider.routerReportsNewRouteInformation( + RouteInformation( + uri: Uri( + path: 'initial', + queryParameters: {'a': 'ws/abcd'}, // This will be escaped. + ), + ), + ); + expect(provider.value.uri.toString(), 'initial?a=ws%2Fabcd'); + // should use `replace: true` + expect(log, [ + isMethodCall('selectMultiEntryHistory', arguments: null), + isMethodCall('routeInformationUpdated', arguments: { 'uri': 'initial?a=ws%2Fabcd', 'state': null, 'replace': true }), + ]); + log.clear(); + + provider.routerReportsNewRouteInformation( + RouteInformation(uri: Uri.parse('initial?a=1&b=2')), + ); + log.clear(); + + // Change query parameters order + provider.routerReportsNewRouteInformation( + RouteInformation(uri: Uri.parse('initial?b=2&a=1')), + ); + // should use `replace: true` + expect(log, [ + isMethodCall('selectMultiEntryHistory', arguments: null), + isMethodCall('routeInformationUpdated', arguments: { 'uri': 'initial?b=2&a=1', 'state': null, 'replace': true }), + ]); + log.clear(); + + provider.routerReportsNewRouteInformation( + RouteInformation(uri: Uri.parse('initial?a=1&a=2')), + ); + log.clear(); + + // Change query parameters order for same key + provider.routerReportsNewRouteInformation( + RouteInformation(uri: Uri.parse('initial?a=2&a=1')), + ); + // should use `replace: true` + expect(log, [ + isMethodCall('selectMultiEntryHistory', arguments: null), + isMethodCall('routeInformationUpdated', arguments: { 'uri': 'initial?a=2&a=1', 'state': null, 'replace': true }), + ]); + log.clear(); + }); + testWidgets('RootBackButtonDispatcher works', (WidgetTester tester) async { final BackButtonDispatcher outerDispatcher = RootBackButtonDispatcher(); final RouteInformationProvider provider = PlatformRouteInformationProvider(