Skip to content

Commit 83ae18a

Browse files
authored
[go_router] add initial json support #110781 (#9404)
Add initial json support for use in go_router_builder Adds annotation that enable custom string encoder/decoder, its enable conversion for base64 This allow custom type conversion for parameters, like mentionated in [#117261](flutter/flutter#117261) and [#110781](flutter/flutter#110781) this PR must made before [#117261](#8665) ## Pre-Review Checklist [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 342356e commit 83ae18a

File tree

5 files changed

+67
-2
lines changed

5 files changed

+67
-2
lines changed

packages/go_router/CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 16.1.0
2+
- Adds annotation for go_router_builder that enable custom string encoder/decoder [#110781](https://github.com/flutter/flutter/issues/110781). **Requires go_router_builder >= 3.1.0**.
3+
14
## 16.0.0
25

36
- **BREAKING CHANGE**
@@ -35,7 +38,7 @@
3538
## 14.8.1
3639

3740
- Secured canPop method for the lack of matches in routerDelegate's configuration.
38-
41+
3942
## 14.8.0
4043

4144
- Adds `preload` parameter to `StatefulShellBranchData.$branch`.

packages/go_router/lib/go_router.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export 'src/configuration.dart';
1111
export 'src/delegate.dart';
1212
export 'src/information_provider.dart';
1313
export 'src/match.dart' hide RouteMatchListCodec;
14+
export 'src/misc/custom_parameter.dart';
1415
export 'src/misc/errors.dart';
1516
export 'src/misc/extensions.dart';
1617
export 'src/misc/inherited_router.dart';
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:meta/meta_meta.dart' show Target, TargetKind;
6+
7+
/// annotation to define a custom parameter decoder/encoder
8+
/// this is useful when the is encoded/decoded in a non-standard way like base64Url
9+
/// this must be used as an annotation on a field
10+
/// ```dart
11+
/// String fromBase64(String value) {
12+
/// return const Utf8Decoder()
13+
/// .convert(base64Url.decode(base64Url.normalize(value)));
14+
/// }
15+
/// String toBase64(String value) {
16+
/// return base64Url.encode(const Utf8Encoder().convert(value));
17+
/// }
18+
/// @TypedGoRoute<JsonRoute>(path: 'json')
19+
/// class JsonRoute extends GoRouteData with _$EncodedRoute {
20+
/// @CustomParameterCodec(
21+
/// encode: toBase64,
22+
/// decode: fromBase64,
23+
/// )
24+
/// final String data;
25+
/// JsonRoute(this.data);
26+
/// }
27+
/// ```
28+
@Target(<TargetKind>{TargetKind.field})
29+
final class CustomParameterCodec {
30+
/// create a custom parameter codec
31+
///
32+
const CustomParameterCodec({
33+
required this.encode,
34+
required this.decode,
35+
});
36+
37+
/// custom function to encode the field
38+
final String Function(String field) encode;
39+
40+
/// custom function to decode the field
41+
final String Function(String field) decode;
42+
}

packages/go_router/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: go_router
22
description: A declarative router for Flutter based on Navigation 2 supporting
33
deep linking, data-driven routes and more
4-
version: 16.0.0
4+
version: 16.1.0
55
repository: https://github.com/flutter/packages/tree/main/packages/go_router
66
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22
77

packages/go_router/test/route_data_test.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
import 'dart:async';
6+
import 'dart:convert';
67

78
import 'package:flutter/material.dart';
89
import 'package:flutter_test/flutter_test.dart';
@@ -226,6 +227,14 @@ final List<GoRoute> _routes = <GoRoute>[
226227
_goRouteDataRedirect,
227228
];
228229

230+
String fromBase64(String value) {
231+
return const Utf8Decoder().convert(base64.decode(value));
232+
}
233+
234+
String toBase64(String value) {
235+
return base64.encode(const Utf8Encoder().convert(value));
236+
}
237+
229238
void main() {
230239
group('GoRouteData', () {
231240
testWidgets(
@@ -633,4 +642,14 @@ void main() {
633642
),
634643
);
635644
});
645+
646+
test('CustomParameterCodec with required parameters', () {
647+
const CustomParameterCodec customParameterCodec = CustomParameterCodec(
648+
encode: toBase64,
649+
decode: fromBase64,
650+
);
651+
652+
expect(customParameterCodec.encode, toBase64);
653+
expect(customParameterCodec.decode, fromBase64);
654+
});
636655
}

0 commit comments

Comments
 (0)