-
Notifications
You must be signed in to change notification settings - Fork 127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add getParams method to router.dart #453
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -190,6 +190,22 @@ class Router { | |
return _notFoundHandler(request); | ||
} | ||
|
||
/// Get URL parameters captured by the [Router]. | ||
/// | ||
/// Returns `null` if no parameters are captured. | ||
Map<String, String>? getParams(Request request) { | ||
for (var route in _routes) { | ||
if (route.verb != request.method.toUpperCase() && route.verb != 'ALL') { | ||
continue; | ||
} | ||
var params = route.match('/${request.url.path}'); | ||
if (params != null) { | ||
return params; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Technically, the route is not necessarily matched. See documentation for It's kind of weird to look at matched parameters from different routes and mounted routers. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's true, but only when a route was matched and found are the params returned. Therefore you can deduce whether the router found a route or not (see also my comment below). |
||
} | ||
} | ||
return null; | ||
} | ||
|
||
// Handlers for all methods | ||
|
||
/// Handle `GET` request to [route] using [handler]. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we decide to land this, how about the name
urlParameters
.https://dart.dev/effective-dart/design#avoid-starting-a-method-name-with-get