Skip to content

[go_router_builder] Proposal: add json support, custom string encoder/decoder #8665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions packages/go_router/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 15.2.1

- 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.0.1**.

## 15.2.0

- `GoRouteData` now defines `.location`, `.go(context)`, `.push(context)`, `.pushReplacement(context)`, and `replace(context)` to be used for [Type-safe routing](https://pub.dev/documentation/go_router/latest/topics/Type-safe%20routes-topic.html). **Requires go_router_builder >= 3.0.0**.
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
40 changes: 40 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,40 @@
// 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';

/// annotation to define a custom parameter decoder/encoder
/// this is useful when the is encoded/decoded in a non-standard way like base64
/// this must be used as an annotation on a field
/// ```dart
/// String fromBase64(String value) {
/// return const Utf8Decoder().convert(base64.decode(value));
/// }
/// String toBase64(String value) {
/// return base64.encode(const Utf8Encoder().convert(value));
/// }
/// class MyRoute {
/// @CustomParameterCodec(
/// encode: toBase64,
/// decode: fromBase64,
/// )
/// final String data;
/// MyRoute(this.data);
/// }
/// ```
@Target({TargetKind.field})
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 json) encode;

/// custom function to decode the field
final String Function(String json) 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: 15.2.0
version: 15.2.1
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
5 changes: 5 additions & 0 deletions packages/go_router_builder/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 3.0.1

- Adds support for classes that support fromJson/toJson. [#117261](https://github.com/flutter/flutter/issues/117261)
- Adds annotation that enable custom string encoder/decoder [#110781](https://github.com/flutter/flutter/issues/110781)

## 3.0.0

- Route classes now required to use a mixin `with _$RouteName`.
Expand Down
89 changes: 89 additions & 0 deletions packages/go_router_builder/example/lib/json_example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// 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.

// ignore_for_file: public_member_api_docs, unreachable_from_main

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

import 'shared/data.dart';

part 'json_example.g.dart';

void main() => runApp(App());

class App extends StatelessWidget {
App({super.key});

@override
Widget build(BuildContext context) => MaterialApp.router(
routerConfig: _router,
title: _appTitle,
);

final GoRouter _router = GoRouter(routes: $appRoutes);
}

@TypedGoRoute<HomeRoute>(
path: '/',
name: 'Home',
routes: <TypedGoRoute<GoRouteData>>[
TypedGoRoute<JsonRoute>(path: 'json'),
],
)
class HomeRoute extends GoRouteData with _$HomeRoute {
const HomeRoute();

@override
Widget build(BuildContext context, GoRouterState state) => const HomeScreen();
}

class JsonRoute extends GoRouteData with _$JsonRoute {
const JsonRoute(this.json);

final JsonExample json;

@override
Widget build(BuildContext context, GoRouterState state) =>
JsonScreen(json: json);
}

class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});

@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text(_appTitle)),
body: ListView(
children: <Widget>[
for (final JsonExample json in jsonData)
ListTile(
title: Text(json.name),
onTap: () => JsonRoute(json).go(context),
)
],
),
);
}

class JsonScreen extends StatelessWidget {
const JsonScreen({required this.json, super.key});
final JsonExample json;

@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: Text(json.name)),
body: ListView(
key: ValueKey<String>(json.id),
children: <Widget>[
Text(json.id),
Text(json.name),
],
),
);
}

const String _appTitle = 'GoRouter Example: builder';
79 changes: 79 additions & 0 deletions packages/go_router_builder/example/lib/json_example.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions packages/go_router_builder/example/lib/shared/data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,46 @@ class Repository {
return FamilyPerson(family: family, person: family.person(pid));
}
}

class JsonExample {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's move this to json_example.dart

const JsonExample({required this.id, required this.name});

factory JsonExample.fromJson(Map<String, dynamic> json) {
return JsonExample(
id: json['id'] as String,
name: json['name'] as String,
);
}

Map<String, dynamic> toJson() {
return <String, dynamic>{
'id': id,
'name': name,
};
}

final String id;
final String name;
}

const List<JsonExample> jsonData = <JsonExample>[
JsonExample(id: '1', name: 'people'),
JsonExample(id: '2', name: 'animals'),
];

class JsonExampleNested<T> {
const JsonExampleNested({required this.child});

factory JsonExampleNested.fromJson(
Map<String, dynamic> json,
T Function(Object? json) fromJsonT,
) {
return JsonExampleNested<T>(child: fromJsonT(json['child']));
}

Map<String, dynamic> toJson() {
return <String, dynamic>{'child': child};
}

final T child;
}
14 changes: 12 additions & 2 deletions packages/go_router_builder/lib/src/route_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,12 @@ class GoRouteConfig extends RouteBaseConfig {
);
}
}
final String fromStateExpression = decodeParameter(element, _pathParams);
final List<ElementAnnotation>? metadata = _fieldMetadata(element.name);
final String fromStateExpression = decodeParameter(
element,
_pathParams,
metadata,
);

if (element.isPositional) {
return '$fromStateExpression,';
Expand All @@ -328,7 +333,8 @@ class GoRouteConfig extends RouteBaseConfig {
);
}

return encodeField(field);
final List<ElementAnnotation>? metadata = _fieldMetadata(fieldName);
return encodeField(field, metadata);
}

String get _locationQueryParams {
Expand Down Expand Up @@ -752,6 +758,10 @@ $routeDataClassName.$dataConvertionFunctionName(
PropertyAccessorElement? _field(String name) =>
routeDataClass.getGetter(name);

List<ElementAnnotation>? _fieldMetadata(String name) => routeDataClass.fields
.firstWhereOrNull((FieldElement element) => element.displayName == name)
?.metadata;

/// The name of `RouteData` subclass this configuration represents.
@protected
String get routeDataClassName;
Expand Down
Loading