diff --git a/packages/go_router/lib/src/path_utils.dart b/packages/go_router/lib/src/path_utils.dart index ed1ce7cc2015..04082994abd0 100644 --- a/packages/go_router/lib/src/path_utils.dart +++ b/packages/go_router/lib/src/path_utils.dart @@ -85,7 +85,11 @@ String patternToPath(String pattern, Map pathParameters) { buffer.write(pattern.substring(start, match.start)); } final String name = match[1]!; - buffer.write(pathParameters[name]); + final String? value = pathParameters[name]; + if (value == null) { + throw GoException('Missing path parameter: $name for pattern "$pattern"'); + } + buffer.write(value); start = match.end; } diff --git a/packages/go_router/pending_changelogs/fix_pattern_to_path_throws.yaml b/packages/go_router/pending_changelogs/fix_pattern_to_path_throws.yaml new file mode 100644 index 000000000000..fd0702968c65 --- /dev/null +++ b/packages/go_router/pending_changelogs/fix_pattern_to_path_throws.yaml @@ -0,0 +1,3 @@ +changelog: | + - Fixes `patternToPath` to throw `GoException` when a path parameter is missing instead of producing URLs containing the literal "null". +version: patch diff --git a/packages/go_router/test/path_utils_test.dart b/packages/go_router/test/path_utils_test.dart index 6dbf91102676..3bbbb13a632a 100644 --- a/packages/go_router/test/path_utils_test.dart +++ b/packages/go_router/test/path_utils_test.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. import 'package:flutter_test/flutter_test.dart'; +import 'package:go_router/go_router.dart' show GoException; import 'package:go_router/src/path_utils.dart'; void main() { @@ -95,6 +96,19 @@ void main() { expect(url, restoredUrl); }); + test('patternToPath throws when path parameter is missing', () { + const pattern = '/user/:id/book/:bookId'; + final incompleteParams = {'id': '123'}; + expect( + () => patternToPath(pattern, incompleteParams), + throwsA(isA().having( + (GoException e) => e.message, + 'message', + contains('Missing path parameter: bookId'), + )), + ); + }); + test('concatenatePaths', () { void verify(String pathA, String pathB, String expected) { final String result = concatenatePaths(pathA, pathB);