-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[go_router] Adds an ability to add a custom codec for serializing/des… #5288
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/go_router/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// 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 'dart:convert'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:go_router/go_router.dart'; | ||
|
||
/// This sample app demonstrates how to provide a codec for complex extra data. | ||
void main() => runApp(const MyApp()); | ||
|
||
/// The router configuration. | ||
final GoRouter _router = GoRouter( | ||
routes: <RouteBase>[ | ||
GoRoute( | ||
path: '/', | ||
builder: (BuildContext context, GoRouterState state) => | ||
const HomeScreen(), | ||
), | ||
], | ||
extraCodec: const MyExtraCodec(), | ||
); | ||
|
||
/// The main app. | ||
class MyApp extends StatelessWidget { | ||
/// Constructs a [MyApp] | ||
const MyApp({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp.router( | ||
routerConfig: _router, | ||
); | ||
} | ||
} | ||
|
||
/// The home screen. | ||
class HomeScreen extends StatelessWidget { | ||
/// Constructs a [HomeScreen]. | ||
const HomeScreen({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar(title: const Text('Home Screen')), | ||
body: Center( | ||
child: Column( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: <Widget>[ | ||
const Text( | ||
"If running in web, use the browser's backward and forward button to test extra codec after setting extra several times."), | ||
Text( | ||
'The extra for this page is: ${GoRouterState.of(context).extra}'), | ||
ElevatedButton( | ||
onPressed: () => context.go('/', extra: ComplexData1('data')), | ||
child: const Text('Set extra to ComplexData1'), | ||
), | ||
ElevatedButton( | ||
onPressed: () => context.go('/', extra: ComplexData2('data')), | ||
child: const Text('Set extra to ComplexData2'), | ||
), | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} | ||
|
||
/// A complex class. | ||
class ComplexData1 { | ||
/// Create a complex object. | ||
ComplexData1(this.data); | ||
|
||
/// The data. | ||
final String data; | ||
|
||
@override | ||
String toString() => 'ComplexData1(data: $data)'; | ||
} | ||
|
||
/// A complex class. | ||
class ComplexData2 { | ||
/// Create a complex object. | ||
ComplexData2(this.data); | ||
|
||
/// The data. | ||
final String data; | ||
|
||
@override | ||
String toString() => 'ComplexData2(data: $data)'; | ||
} | ||
|
||
/// A codec that can serialize both [ComplexData1] and [ComplexData2]. | ||
class MyExtraCodec extends Codec<Object?, Object?> { | ||
/// Create a codec. | ||
const MyExtraCodec(); | ||
@override | ||
Converter<Object?, Object?> get decoder => const _MyExtraDecoder(); | ||
|
||
@override | ||
Converter<Object?, Object?> get encoder => const _MyExtraEncoder(); | ||
} | ||
|
||
class _MyExtraDecoder extends Converter<Object?, Object?> { | ||
const _MyExtraDecoder(); | ||
@override | ||
Object? convert(Object? input) { | ||
if (input == null) { | ||
return null; | ||
} | ||
final List<Object?> inputAsList = input as List<Object?>; | ||
if (inputAsList[0] == 'ComplexData1') { | ||
return ComplexData1(inputAsList[1]! as String); | ||
} | ||
if (inputAsList[0] == 'ComplexData2') { | ||
return ComplexData2(inputAsList[1]! as String); | ||
} | ||
throw FormatException('Unable tp parse input: $input'); | ||
} | ||
} | ||
|
||
class _MyExtraEncoder extends Converter<Object?, Object?> { | ||
const _MyExtraEncoder(); | ||
@override | ||
Object? convert(Object? input) { | ||
if (input == null) { | ||
return null; | ||
} | ||
switch (input.runtimeType) { | ||
case ComplexData1: | ||
return <Object?>['ComplexData1', (input as ComplexData1).data]; | ||
case ComplexData2: | ||
return <Object?>['ComplexData2', (input as ComplexData2).data]; | ||
default: | ||
throw FormatException('Cannot encode type ${input.runtimeType}'); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// 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:flutter_test/flutter_test.dart'; | ||
import 'package:go_router_examples/extra_codec.dart' as example; | ||
|
||
void main() { | ||
testWidgets('example works', (WidgetTester tester) async { | ||
await tester.pumpWidget(const example.MyApp()); | ||
expect(find.text('The extra for this page is: null'), findsOneWidget); | ||
|
||
await tester.tap(find.text('Set extra to ComplexData1')); | ||
await tester.pumpAndSettle(); | ||
expect(find.text('The extra for this page is: ComplexData1(data: data)'), | ||
findsOneWidget); | ||
|
||
await tester.tap(find.text('Set extra to ComplexData2')); | ||
await tester.pumpAndSettle(); | ||
expect(find.text('The extra for this page is: ComplexData2(data: data)'), | ||
findsOneWidget); | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe link the "extra" to GoRoute.extra or wherever users can learn more about what "extra" is.