RoutePattern class
Represents a route pattern against which request paths will be matched.
RoutePatterns are defined using a specific syntax. They can contain placeholders for parts of the route which can be read by the endpoint. Invalid route patterns will throw an ApiError.
Examples:
/path/to/endpoint
/users/joe
Placeholders:
Placeholders can be used to create a dynamic route pattern. In this example we create the pattern for a user endpoint:
Examples:
/users/{name}
/users/{name}/pictures
/texts/article_{article_id}
The placeholder {name}
will allow anything as long as it is a single
path segment.
Placeholder keys can only contain the characters a-z, 0-9, _, -
. No white
space, slashes or other special characters allowed.
For the route pattern
/users/{name}/pictures
the following path will match:
/users/joe/pictures
and the following will not:
/users/pictures
/users/two/segments/pictures
Multiple Placeholders can be used in a single route and placeholders
can also have a prefix:
/category/{category_name}/article_{article_id}
Against the example pattern above following routes will match:
/category/fiction/article_2354
/category/science/article_abc
while the following will not:
/category/article_2354
/category/science/article_
For more flexible matching, placeholders can also be defined as optional by appending a question mark to the name:
/articles/{articleId?}
Wildcard-Suffix
All of the examples above are closed routes, which means that they have to match in full length with the path to match at all.
Example route:
/path/{x}/something
This path will match:
/path/to/something
This one will not:
/path/to/something/else
If sub-paths are required to match the pattern also, a wildcard suffix can be used. Wildcard suffix only work at the end of the pattern!
Example route:
/path/{x}/something/*
This paths will match:
/path/to/something
/path/to/something/else
/path/to/something/else/and/more
The segments matched by the wildcard-suffix is also stored in the Route object. See Route.wildcard
Constructors
- RoutePattern(String pattern)
-
factory
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- isWildcardPattern → bool
-
final
- pattern → String
-
final
- routeMatchExp → RegExp
-
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
assertParam(
String param, {bool? isOptional}) → void -
Throws ApiError if
param
does not exist as route parameter in this pattern. -
containsParam(
String key) → bool - Checks if there is a placeholder param with the given key in the pattern.
-
decode(
String path) → Route - Decodes a path using the route pattern.
-
encode(
Map< String, dynamic> values) → String - Encodes url params into a path.
-
isOptionalParam(
String key) → bool - Checks whether the placeholder with the given key is optional.
-
match(
String path) → bool -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- any → RoutePattern
-
final