Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/go_router/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 16.1.0
- 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**.

## 16.0.0

- **BREAKING CHANGE**
Expand Down Expand Up @@ -35,7 +38,7 @@
## 14.8.1

- Secured canPop method for the lack of matches in routerDelegate's configuration.

## 14.8.0

- Adds `preload` parameter to `StatefulShellBranchData.$branch`.
Expand Down
1 change: 1 addition & 0 deletions packages/go_router/lib/go_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export 'src/configuration.dart';
export 'src/delegate.dart';
export 'src/information_provider.dart';
export 'src/match.dart' hide RouteMatchListCodec;
export 'src/misc/custom_parameter.dart';
export 'src/misc/errors.dart';
export 'src/misc/extensions.dart';
export 'src/misc/inherited_router.dart';
Expand Down
42 changes: 42 additions & 0 deletions packages/go_router/lib/src/misc/custom_parameter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:meta/meta_meta.dart' show Target, TargetKind;

/// annotation to define a custom parameter decoder/encoder
/// this is useful when the is encoded/decoded in a non-standard way like base64Url
/// this must be used as an annotation on a field
/// ```dart
/// String fromBase64(String value) {
/// return const Utf8Decoder()
/// .convert(base64Url.decode(base64Url.normalize(value)));
/// }
/// String toBase64(String value) {
/// return base64Url.encode(const Utf8Encoder().convert(value));
/// }
/// @TypedGoRoute<JsonRoute>(path: 'json')
/// class JsonRoute extends GoRouteData with _$EncodedRoute {
/// @CustomParameterCodec(
/// encode: toBase64,
/// decode: fromBase64,
/// )
/// final String data;
/// JsonRoute(this.data);
/// }
/// ```
@Target(<TargetKind>{TargetKind.field})
final class CustomParameterCodec {
/// create a custom parameter codec
///
const CustomParameterCodec({
required this.encode,
required this.decode,
});

/// custom function to encode the field
final String Function(String field) encode;

/// custom function to decode the field
final String Function(String field) decode;
}
2 changes: 1 addition & 1 deletion packages/go_router/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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: 16.0.0
version: 16.1.0
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

Expand Down
19 changes: 19 additions & 0 deletions packages/go_router/test/route_data_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
Expand Down Expand Up @@ -226,6 +227,14 @@ final List<GoRoute> _routes = <GoRoute>[
_goRouteDataRedirect,
];

String fromBase64(String value) {
return const Utf8Decoder().convert(base64.decode(value));
}

String toBase64(String value) {
return base64.encode(const Utf8Encoder().convert(value));
}

void main() {
group('GoRouteData', () {
testWidgets(
Expand Down Expand Up @@ -633,4 +642,14 @@ void main() {
),
);
});

test('CustomParameterCodec with required parameters', () {
const CustomParameterCodec customParameterCodec = CustomParameterCodec(
encode: toBase64,
decode: fromBase64,
);

expect(customParameterCodec.encode, toBase64);
expect(customParameterCodec.decode, fromBase64);
});
}