Repository navigation
Closed
flutter/packages
#1995Labels
P3Issues that are less important to the Flutter projectc: proposalA detailed proposal for a change to Flutterp: go_routerThe go_router packagepackageflutter/packages repository. See also p: labels.
Description
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.
Proposal
This can be accomplished by removing go_router_delegate.dart l442-l463.
441
442 if (matchStacks.length > 1) {
443 final sb = StringBuffer()
444 ..writeln('too many routes for location: $location');
445
446 for (final stack in matchStacks) {
447 sb.writeln('\t${stack.map((m) => m.route.path).join(' => ')}');
448 }
449
450 throw Exception(sb.toString());
451 }
452
453 if (kDebugMode) {
454 assert(matchStacks.length == 1);
455 final match = matchStacks.first.last;
456 final loc1 = _addQueryParams(match.subloc, match.queryParams);
457 final uri2 = Uri.parse(location);
458 final loc2 = _addQueryParams(uri2.path, uri2.queryParameters);
459
460 // NOTE: match the lower case, since subloc is canonicalized to match the
461 // path case whereas the location can be any case
462 assert(loc1.toLowerCase() == loc2.toLowerCase(), '$loc1 != $loc2');
463 }
442
Reacted by Alexandru Mariuti, Katz, Jason C.H, Kurogoma4D, Sheena, zolkiBy, Daniel Kerwin, rossheat, Stefan Humm, Yanuar Arifin and 3 more
Metadata
Metadata
Assignees
Labels
P3Issues that are less important to the Flutter projectc: proposalA detailed proposal for a change to Flutterp: go_routerThe go_router packagepackageflutter/packages repository. See also p: labels.
Type
Projects
- StatusShow more project fieldsDone
Activity
nank1ro commentedon Mar 17, 2022
Really need this, would love if you do this.
Please take a look at next js dynamic links documentation: https://nextjs.org/docs/routing/dynamic-routes#caveats
They say:
I completely agree with this behavior.
rossheat commentedon Apr 2, 2022
I wholeheartedly agree with the above sentiments.
Right now, the following commonly seen URLs are not possible:
twitter.com/[username]
instagram.com/[username]
etc.
RaduGrama commentedon Apr 5, 2022
Agree with the proposed behavior, most common routers pick the first found route, allowing the developer to prioritize.
rhc commentedon Apr 15, 2022
Same here. Coming from a Web background, the ability to distinguish /projects/new and /projects/:id is crucial
GauravCalidig commentedon May 19, 2022
Same here, I guess its just a matter of commenting code
GauravCalidig commentedon May 19, 2022
Created a dummy repo here if anyone wants this and can't wait for package update.
Fixes : flutter/flutter#99833
amerblackbird commentedon May 26, 2022
Its crucial feature
github-actions commentedon Jun 10, 2022
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 -vand a minimal reproduction of the issue.