Skip to content

[go_router] If there is more than one route to match, use the first match. - #1995

Merged
fluttergithubbot merged 8 commits into
flutter:mainfrom
GauravCalidig:main
May 27, 2022
Merged

fluttergithubbot merged 8 commits into
flutter:mainfrom
GauravCalidig:main

Conversation

@GauravCalidig

Copy link
Copy Markdown
Contributor

Use case
If there is more than one route to match, use the first match.

The following was implemented using go_router.

final _router = GoRouter(
  routes: [
    GoRoute(
      path: '/home',
      builder: (_, __) => const HomePage(),
      routes: [
        GoRoute(
          path: 'create',
          builder: (_, __) => const CreatePage(),
        ),
        GoRoute(
          path: ':id',
          builder: (_, __) => const DetailPage(),
        ),
    ),
  ],
  urlPathStrategy: UrlPathStrategy.path,
);

I expected the following behavior.

/home/create -> CreatePage
/home/xxxx -> DetailPage ("xxxx" is some string, but exclude "create")
But I opened /home/create, go_router displayed following error.

Exception: too many routes for location: /home/create
/home => create
/home => :id
go_router uses path_to_regexp internally.
Therefore, perhaps specifying a regular expression could solve this problem. However, we felt that this would not be concise and would be a hassle for the developer.

Fixes : flutter/flutter#99833

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.

@flutter-dashboard

Copy link
Copy Markdown

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat (don't just cc him here, he won't see it! He's on Discord!).

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@google-cla

google-cla Bot commented May 19, 2022

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

For more information, open the CLA check for this pull request.

@chunhtai chunhtai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It looks like there are some format change the breaks the ci. Can you revert them.

Removing the assert looks like a reasonable change, but can you add a test to verify the it does not throw if there are multiple matches?

Comment thread packages/go_router/CHANGELOG.md Resolved
@chunhtai

Copy link
Copy Markdown
Contributor

build failed

00:12 +21 -1: /tmp/cirrus-ci-build/packages/go_router/test/go_router_test.dart: path routes match too many sub-routes [E]                                                                              
  Expected: an object with length of <1>
    Actual: [GoRouteMatch:GoRouteMatch(/, {}), GoRouteMatch:GoRouteMatch(/foo/bar, {})]
     Which: has length of <2>
  
  package:test_api                                    expect
  package:flutter_test/src/widget_tester.dart 455:16  expect
  test/go_router_test.dart 305:7                      main.<fn>.<fn>
  

00:12 +21 -1: loading /tmp/cirrus-ci-build/packages/go_router/test/go_router_test.dart         

@chunhtai chunhtai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, left some comments

Comment thread packages/go_router/CHANGELOG.md Resolved
@chunhtai
chunhtai requested a review from johnpryan May 26, 2022 16:38
@chunhtai

Copy link
Copy Markdown
Contributor

adding secondary reviewer

@fluttergithubbot
fluttergithubbot merged commit 197bb11 into flutter:main May 27, 2022
creatorpiyush pushed a commit to creatorpiyush/packages that referenced this pull request Jun 10, 2026
bisor0627 pushed a commit to bisor0627/packages that referenced this pull request Jun 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[go_router]If there is more than one route to match, use the first match

4 participants