packages
packages copied to clipboard
[go_router] Make `queryParams` a `Map<String, dynamic>`
fixes https://github.com/flutter/flutter/issues/108390
Pre-launch Checklist
- [x] I read the Contributor Guide and followed the process outlined there for submitting PRs.
- [x] I read the Tree Hygiene wiki page, which explains my responsibilities.
- [x] I read and followed the relevant style guides and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages repo does use
dart format
.) - [x] I signed the CLA.
- [x] The title of the PR starts with the name of the package surrounded by square brackets, e.g.
[shared_preferences]
- [x] I listed at least one issue that this PR fixes in the description above.
- [x] I updated
pubspec.yaml
with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes. - [x] I updated
CHANGELOG.md
to add a description of the change, following repository CHANGELOG style. - [x] I updated/added relevant documentation (doc comments with
///
). - [x] I added new tests to check the change I am making, or this PR is test-exempt.
- [x] All existing and new tests are passing.
If you need help, consider asking for advice on the #hackers-new channel on Discord.
@chunhtai I should have fixed the linter now
Good idea, in my job when we need to give a widget a parameter that's not a String we use extra
as a Map to pass the variables. This makes a lot of sense.
Hello @chunhtai , is there something else I should do about this PR ?
Just confirming, there's still work to do in go_router_builder (https://github.com/flutter/flutter/issues/108437), but this won't break it?
Just confirming, there's still work to do in go_router_builder (flutter/flutter#108437), but this won't break it?
Hum, I might be wrong but I think we are good. Here we are
- Allowing the user to pass
Map<String, dynamic>
instead ofMap<String, String>
togoNamed
. ButgoNamed
is not used by the generated code ofgo_router_builder
(I think). It generates the location itself with thequeryParameters
(usingUri
) and uses.go()
andpush()
. - Adding a new getter/parameter
queryParametersAll
but I didn't modify anything forqueryParams
so the code generated bygo_router_builder
(_fromState
) should still be able to use it.
But yeah, in order to do https://github.com/flutter/flutter/issues/108437, we should merge this PR first, so GoRouter
can support multiple instances of the same key inside the url. Then go_router_builder
can be changed to support Iterable
s.
@johnpryan Do you want me to do anything else on this PR?
cc @johnpryan
@chunhtai Isn't this technically a breaking change since we changed method signatures? Should we target the v5 branch instead?
@loic-sharma I don't think this will be breaking you can upward cast type in map without addition code. This should work fine.
Map<String, dynamic> queryParams = <String, String>{};
Or is there a use case I may be missing?
Yup never mind my concern. This works as expected too:
class Test {
void go(Map<String, dynamic> queryParams) {
print(queryParams);
}
}
typedef Go = void Function(Map<String, String> queryParams);
void main() async {
var test = Test();
Go go = test.go;
go({'foo': 'bar'});
}
i see you already support map<String, dynamic> but queryParams in GoRouterState is still map<String, String> so i can't get data, it throws type 'SomethingLikeEnum' is not a subtype of type 'IterableGoRoute( name: AppRouterName.resendEmailPage, path: '/${AppRouterName.resendEmailPage}', builder: (context, state) { SomethingLikeEnum data = state.queryParams["data"] as SomethingLikeEnum; return ResendEmailPage(data: data); }, ),
@Sotatek-PhiPham You can use GoRouterState.queryParametersAll
which is Map<String, dynamic>
i tried queryParametersAll but it is Map<String, List<String>>. I am using go router version 4.4.1
Yes, it uses Uri
and this is how it works:
Uri.parse()
takes Map<String, dynamic>? queryParameters
And Uri
's getters are:
-
Map<String, String> queryParameters
-
Map<String, List<String>> queryParametersAll
still cant get bool in queryParameters , i need bool.parse to convert string to bool , is this possible to make queryParameters getter to <String, dynamic> ?
the dynamic is not meant to be use for various type. It is to support list query parameter. https://api.flutter.dev/flutter/dart-core/Uri/queryParametersAll.html
locking this issue