[go_router] Fixes namedLocation to return URIs without trailing question marks if there are no query parameters. (#2471)

diff --git a/packages/go_router/CHANGELOG.md b/packages/go_router/CHANGELOG.md
index 471351f..69b1b70 100644
--- a/packages/go_router/CHANGELOG.md
+++ b/packages/go_router/CHANGELOG.md
@@ -1,10 +1,11 @@
-## NEXT
+## 4.2.8
 
-- Cleans up examples
+- Fixes namedLocation to return URIs without trailing question marks if there are no query parameters.
+- Cleans up examples.
 
 ## 4.2.7
 
-- Update README
+- Updates README.
 
 ## 4.2.6
 
diff --git a/packages/go_router/lib/src/configuration.dart b/packages/go_router/lib/src/configuration.dart
index 321541d..c11c230 100644
--- a/packages/go_router/lib/src/configuration.dart
+++ b/packages/go_router/lib/src/configuration.dart
@@ -80,7 +80,10 @@
         param.key: Uri.encodeComponent(param.value)
     };
     final String location = patternToPath(path, encodedParams);
-    return Uri(path: location, queryParameters: queryParams).toString();
+    return Uri(
+            path: location,
+            queryParameters: queryParams.isEmpty ? null : queryParams)
+        .toString();
   }
 
   @override
diff --git a/packages/go_router/pubspec.yaml b/packages/go_router/pubspec.yaml
index 6f6a2db..44cda90 100644
--- a/packages/go_router/pubspec.yaml
+++ b/packages/go_router/pubspec.yaml
@@ -1,7 +1,7 @@
 name: go_router
 description: A declarative router for Flutter based on Navigation 2 supporting
   deep linking, data-driven routes and more
-version: 4.2.7
+version: 4.2.8
 repository: https://github.com/flutter/packages/tree/main/packages/go_router
 issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22
 
diff --git a/packages/go_router/test/parser_test.dart b/packages/go_router/test/parser_test.dart
index 5d16483..8d119b2dc 100644
--- a/packages/go_router/test/parser_test.dart
+++ b/packages/go_router/test/parser_test.dart
@@ -92,12 +92,26 @@
       topRedirect: (_) => null,
     );
 
-    expect(configuration.namedLocation('lowercase'), '/abc?');
-    expect(configuration.namedLocation('LOWERCASE'), '/abc?');
-    expect(configuration.namedLocation('camelCase'), '/efg?');
-    expect(configuration.namedLocation('camelcase'), '/efg?');
-    expect(configuration.namedLocation('snake_case'), '/hij?');
-    expect(configuration.namedLocation('SNAKE_CASE'), '/hij?');
+    expect(configuration.namedLocation('lowercase'), '/abc');
+    expect(configuration.namedLocation('LOWERCASE'), '/abc');
+    expect(configuration.namedLocation('camelCase'), '/efg');
+    expect(configuration.namedLocation('camelcase'), '/efg');
+    expect(configuration.namedLocation('snake_case'), '/hij');
+    expect(configuration.namedLocation('SNAKE_CASE'), '/hij');
+
+    // With query parameters
+    expect(
+        configuration
+            .namedLocation('lowercase', queryParams: const <String, String>{}),
+        '/abc');
+    expect(
+        configuration.namedLocation('lowercase',
+            queryParams: const <String, String>{'q': '1'}),
+        '/abc?q=1');
+    expect(
+        configuration.namedLocation('lowercase',
+            queryParams: const <String, String>{'q': '1', 'g': '2'}),
+        '/abc?q=1&g=2');
   });
 
   test('GoRouteInformationParser returns error when unknown route', () async {