flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
165.75k stars 27.38k forks source link

[go_router] query parameter is retained when the in-app back button is pressed #108381

Closed johnpryan closed 2 years ago

johnpryan commented 2 years ago

Pressing the in-app back button should clear the query parameters

Steps to reproduce:

  1. Run this sample
  2. Press the button to navigate to /b?filter=abc
  3. Press the in-app back button
  4. Observe that the query parameter didn't go away - the url is /?filter=abc but should be /
Skogsfrae commented 2 years ago

The problem seems to be caused by GoRouteInformationParser._getLocRouteRecursively and how it computes the match list.

In each recursion it iterates every GoRoute and creates a new match from it, passing last route query params without taking into account previous match configuration:

lib/src/go_route_information_parser.dart:329

  static List<GoRouteMatch> _getLocRouteRecursively({
    required String loc,
    required String restLoc,
    required String parentSubloc,
    required List<GoRoute> routes,
    required String parentFullpath,
    required Map<String, String> queryParams,
    required Object? extra,
  }) {
    bool debugGatherAllMatches = false;
    assert(() {
      debugGatherAllMatches = true;
      return true;
    }());
    final List<List<GoRouteMatch>> result = <List<GoRouteMatch>>[];
    // find the set of matches at this level of the tree
    for (final GoRoute route in routes) {
      final String fullpath = concatenatePaths(parentFullpath, route.path);
      final GoRouteMatch? match = GoRouteMatch.match(
        route: route,
        restLoc: restLoc,
        parentSubloc: parentSubloc,
        fullpath: fullpath,
        queryParams: queryParams,     <------
        extra: extra,
      );
...
chunhtai commented 2 years ago

I am also not sure what the expected behavior should be, the previous page may rely on the query parameter.

johnpryan commented 2 years ago

If this is the intended behavior we need to specify it somewhere, otherwise we should consider it a bug. I'm not aware of any routing libraries that keep the query parameters after navigation occurs.

chunhtai commented 2 years ago

I think we either keep it, or the query parameters are only visible to the last match, WDYT? @johnpryan

johnpryan commented 2 years ago

After thinking about this some more, this is probably working as intended... Other route matches could be relying on the query parameters so it's not clear that we should clear them in this case.

github-actions[bot] commented 2 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.