Skip to content

Conversation

@ValentinVignal
Copy link
Contributor

@ValentinVignal ValentinVignal commented Jul 27, 2022

fixes flutter/flutter#108390

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • 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.)
  • I signed the CLA.
  • The title of the PR starts with the name of the package surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.
  • I updated CHANGELOG.md to add a description of the change, following repository CHANGELOG style.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@stuartmorgan-g stuartmorgan-g requested a review from chunhtai August 2, 2022 18:48
Copy link
Contributor

@chunhtai chunhtai left a comment

Choose a reason for hiding this comment

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

code looks good, just need a bit more test.

expect(configuration.namedLocation('SNAKE_CASE'), '/hij?');
});

test(
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a test in go_router_test.dart to test the entire flow from context.goNamed() to the actual page is built with the correct query params?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion, while writing the test, I actually faced an issue/bug:

If you run this code snippet:

final uri = Uri(path: 'a/b/', queryParameters: {'q1': 'v1', 'q2': ['v2', 'v3']});
print(uri.queryParametersAll);
print(uri.queryParameters);

It will log

{q1: [v1], q2: [v2, v3]}
{q1: v1, q2: v3}

Because of that, only carrying around queryParams is not enough, I had to add queryParametersAll everywhere.

Do you want me to remove queryParams as the data is now kind of duplicated? This would be a breaking change I think.
Or I could create getters Map<String, dynamic> get queryParams to not duplicate stored data and not introduce a breaking change?

What do you think?

Copy link
Contributor

@chunhtai chunhtai left a comment

Choose a reason for hiding this comment

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

Can you fix the merge error and merge conflict ? also the build need to pass before this can merge


- Allows `Map<String, dynamic>` maps as `queryParams` of `goNamed`, `replacedName`, `pushNamed` and `namedLocation`.

## 4.2.5
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems to be merging error?

Copy link
Contributor Author

@ValentinVignal ValentinVignal Aug 4, 2022

Choose a reason for hiding this comment

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

I fixed the merge conflict in 13e6cb1

I'm working on fixing go_router_builder

@ValentinVignal ValentinVignal requested a review from chunhtai August 4, 2022 16:40
@ValentinVignal
Copy link
Contributor Author

@chunhtai I should have fixed the linter now

@NazarenoCavazzon
Copy link
Contributor

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.

…t-of-query-parameters

# Conflicts:
#	packages/go_router/CHANGELOG.md
@ValentinVignal
Copy link
Contributor Author

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

Copy link
Contributor

@chunhtai chunhtai left a comment

Choose a reason for hiding this comment

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

LGTM, just some nits

/// Query parameters for the matched route.
final Map<String, String> queryParams;

/// Query parameters for the matched route.
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a bit more explanation on whats different between this and queryParams?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried to write a better documentation in d9f9315

Tell me if there is something to change

routes: configuration.routes,
parentFullpath: '',
parentSubloc: '',
// Here, if the request is `a/b/?q1=v1&q2=v2&q2=v3`;
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment feel a bit out of place, maybe put this is doc string of the class property instead?

final GoRouter router = await createRouter(routes, tester);
router.go('/login/');
final List<RouteMatch> matches = router.routerDelegate.matches.matches;
print(matches);
Copy link
Contributor

Choose a reason for hiding this comment

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

nice catch!

@chunhtai chunhtai requested a review from johnpryan August 17, 2022 16:49
@@ -1,5 +1,6 @@
## NEXT
## 4.2.8
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be 4.3.0 since this adds a new parameter to GoRouterState?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed the version number in e722483


final GoRouter router = await createRouter(routes, tester);

router.goNamed('page', queryParams: const <String, dynamic>{
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add a test for go() too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, I added a test in d9f9315

@johnpryan
Copy link
Contributor

Just confirming, there's still work to do in go_router_builder (flutter/flutter#108437), but this won't break it?

@ValentinVignal
Copy link
Contributor Author

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 flutter/flutter#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.

…eters

# Conflicts:
#	packages/go_router/CHANGELOG.md
#	packages/go_router/pubspec.yaml
…t-of-query-parameters

# Conflicts:
#	packages/go_router/CHANGELOG.md
@ValentinVignal
Copy link
Contributor Author

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

@chunhtai
Copy link
Contributor

cc @johnpryan

…t-of-query-parameters

# Conflicts:
#	packages/go_router/CHANGELOG.md
#	packages/go_router/pubspec.yaml
#	packages/go_router/test/test_helpers.dart
@loic-sharma
Copy link
Member

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

@chunhtai
Copy link
Contributor

@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 chunhtai added the autosubmit Merge PR when tree becomes green via auto submit App label Aug 26, 2022
@loic-sharma
Copy link
Member

loic-sharma commented Aug 26, 2022

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

@auto-submit auto-submit bot merged commit a95e400 into flutter:main Aug 26, 2022
@ValentinVignal ValentinVignal deleted the go-router/allow-list-of-query-parameters branch August 27, 2022 07:08
@Sotatek-PhiPham
Copy link

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

@ValentinVignal
Copy link
Contributor Author

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

@Sotatek-PhiPham
Copy link

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

@ValentinVignal
Copy link
Contributor Author

ValentinVignal commented Sep 13, 2022

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

@vrman
Copy link

vrman commented Jun 2, 2023

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

@chunhtai
Copy link
Contributor

chunhtai commented Jun 2, 2023

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

@flutter flutter locked and limited conversation to collaborators Jun 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

autosubmit Merge PR when tree becomes green via auto submit App p: go_router

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[go_router] Support for list of query parameters

7 participants