[go_router] Add encoder,decoder and compare parameters to TypedQueryParameter#11067
[go_router] Add encoder,decoder and compare parameters to TypedQueryParameter#11067ValentinVignal wants to merge 6 commits intoflutter:mainfrom
encoder,decoder and compare parameters to TypedQueryParameter#11067Conversation
encoder,decoder and compare parameters to `TypedQueryParameterencoder,decoder and compare parameters to TypedQueryParameter
There was a problem hiding this comment.
Code Review
This pull request enhances TypedQueryParameter by adding encoder, decoder, and compare parameters, allowing for more flexible handling of query parameters in type-safe routes. The changes are well-implemented and include corresponding updates to the changelog, pubspec, and tests. I have one suggestion to improve the clarity of the documentation for the new compare parameter to prevent potential misuse.
| /// A function that compares two parameter values for equality. | ||
| /// | ||
| /// Returns `true` if the values are different and `false` if they are the | ||
| /// same. This is used to determine whether a parameter should be included in | ||
| /// the URI when it has a default value. |
There was a problem hiding this comment.
The documentation for the compare function is a bit confusing. The first sentence says it's for equality comparison, but the following sentence states it returns true if the values are different. This is counter-intuitive for a function that is described as comparing for equality.
To avoid confusion for developers using this API, I suggest rephrasing the documentation to be more direct about its purpose. For example, you could describe it as an inequality check.
| /// A function that compares two parameter values for equality. | |
| /// | |
| /// Returns `true` if the values are different and `false` if they are the | |
| /// same. This is used to determine whether a parameter should be included in | |
| /// the URI when it has a default value. | |
| /// A function that compares two parameter values for inequality. | |
| /// | |
| /// Returns `true` if the values are considered different, and `false` if they | |
| /// are the same. This is used to determine whether a parameter should be | |
| /// included in the URI when it has a default value. |
|
go_router_builder PR: #11068 |
|
The analyzer is failing because of go_router_builder example Now, |
packages/go_router/CHANGELOG.md
Outdated
| @@ -1,3 +1,7 @@ | |||
| ## 17.2.0 | |||
|
|
|||
| - Adds `encoder`, `decoder` and `compare` parameters to `TypedQueryParameter` annotation for custom encoding, decoding and comparison of query parameters in `TypedGoRoute` constructors. | |||
There was a problem hiding this comment.
go_router has migrated to batch release, now we should add a new entry in pending_changelogs folder. the ci also hinted this.
There was a problem hiding this comment.
Oh okay, I changed it in Use batch release, let me know if that's correct
| /// The returned string is escaped to be URL-safe. For example, a parameter | ||
| /// value of `'field with space'` will generate a query parameter value of | ||
| /// `'field+with+space'`. | ||
| final String? Function(T?)? encoder; |
There was a problem hiding this comment.
we should typedef this. also what does returning null does here? and in what case will null to be feed into this. we should document these behavior in the typedef
There was a problem hiding this comment.
Good point, I made the parameter non-nullable in Better documentation
| /// A function that converts a string from the URI to a parameter value. | ||
| final T? Function(String?)? decoder; | ||
|
|
||
| /// A function that compares two parameter values for equality. |
There was a problem hiding this comment.
can you talk a bit more why this is needed?
There was a problem hiding this comment.
Let me know if that's good enough or not Better documentation
3e95760 to
624a61b
Compare
| /// For example, a parameter value of `'field with space'` will generate a query | ||
| /// parameter value of `'field+with+space'`. | ||
| /// | ||
| /// If the function returns `null`, the parameter will be treated as if it were |
There was a problem hiding this comment.
what is the use case for this?
There was a problem hiding this comment.
I don't think there is really a "use-case"
A typical encoder (with a nullable type) would be something like:
String? encoder(T? value) { // Nullable T here.
if (value == null) return null; // If there is no parameter in the route, we don't want any parameter in the URL
// Encoder logic for non-null T.
}There was a problem hiding this comment.
Maybe the phrasing is not the best?
| /// The [value] parameter contains the encoded string from the URI, or `null` | ||
| /// if the parameter is absent from the URI. | ||
| /// | ||
| /// Return `null` to indicate the parameter was not provided. In this case, |
There was a problem hiding this comment.
Same, can you talk a bit about the use case here?
There was a problem hiding this comment.
I don't think there is a use case per say.
Here, the most common use case would be to return null if String? value is null:
T? myDecoder(String? value) {
if (value == null) return null;
// More logic.
}| /// | ||
| /// Converts an encoded string from the URI into a parameter value of type `T`. | ||
| /// | ||
| /// The [value] parameter contains the encoded string from the URI, or `null` |
There was a problem hiding this comment.
in that case shouldn't this be handled by setting the default value in GoRouteData?
There was a problem hiding this comment.
yes, if there is a default value in GoRouteData being set and the decoder returns true, it will use the default value of GoRouteData :)
| /// | ||
| /// Used to decide whether to include a parameter in the URI when a default | ||
| /// value exists. If the parameter equals its default value, it is omitted | ||
| /// from the URI; otherwise, it is included. |
There was a problem hiding this comment.
should mention this must be provided if the T is not primative type and has default value
There was a problem hiding this comment.
Good point, I added it in Add optionalTypeArgs, assert, and better documentation
Part of
Pre-Review Checklist
[shared_preferences]pubspec.yamlwith an appropriate new version according to the pub versioning philosophy, or I have commented below to indicate which version change exemption this PR falls under1.CHANGELOG.mdto add a description of the change, following repository CHANGELOG style, or I have commented below to indicate which CHANGELOG exemption this PR falls under1.///).If you need help, consider asking for advice on the #hackers-new channel on Discord.
Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the
gemini-code-assistbot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.Footnotes
Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. ↩ ↩2 ↩3