packages icon indicating copy to clipboard operation
packages copied to clipboard

[go_router] Make `queryParams` a `Map<String, dynamic>`

Open ValentinVignal opened this issue 2 years ago • 2 comments

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.

ValentinVignal avatar Jul 27 '22 10:07 ValentinVignal

@chunhtai I should have fixed the linter now

ValentinVignal avatar Aug 04 '22 16:08 ValentinVignal

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.

NazarenoCavazzon avatar Aug 04 '22 23:08 NazarenoCavazzon

Hello @chunhtai , is there something else I should do about this PR ?

ValentinVignal avatar Aug 17 '22 05:08 ValentinVignal

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?

johnpryan avatar Aug 17 '22 17:08 johnpryan

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 of Map<String, String> to goNamed. But goNamed is not used by the generated code of go_router_builder (I think). It generates the location itself with the queryParameters (using Uri) and uses .go() and push().
  • Adding a new getter/parameter queryParametersAll but I didn't modify anything for queryParams so the code generated by go_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 Iterables.

ValentinVignal avatar Aug 18 '22 01:08 ValentinVignal

@johnpryan Do you want me to do anything else on this PR?

ValentinVignal avatar Aug 24 '22 01:08 ValentinVignal

cc @johnpryan

chunhtai avatar Aug 25 '22 21:08 chunhtai

@chunhtai Isn't this technically a breaking change since we changed method signatures? Should we target the v5 branch instead?

loic-sharma avatar Aug 26 '22 20:08 loic-sharma

@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?

chunhtai avatar Aug 26 '22 21:08 chunhtai

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'});
}

loic-sharma avatar Aug 26 '22 22:08 loic-sharma

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 'Iterable' GoRoute( name: AppRouterName.resendEmailPage, path: '/${AppRouterName.resendEmailPage}', builder: (context, state) { SomethingLikeEnum data = state.queryParams["data"] as SomethingLikeEnum; return ResendEmailPage(data: data); }, ),

Sotatek-PhiPham avatar Sep 13 '22 09:09 Sotatek-PhiPham

@Sotatek-PhiPham You can use GoRouterState.queryParametersAll which is Map<String, dynamic>

ValentinVignal avatar Sep 13 '22 14:09 ValentinVignal

i tried queryParametersAll but it is Map<String, List<String>>. I am using go router version 4.4.1

Sotatek-PhiPham avatar Sep 13 '22 14:09 Sotatek-PhiPham

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

ValentinVignal avatar Sep 13 '22 15:09 ValentinVignal

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> ?

vrman avatar Jun 02 '23 23:06 vrman

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

chunhtai avatar Jun 02 '23 23:06 chunhtai