Sub topic 3
Reading a URI: scheme, host, path, and query
A URI (Uniform Resource Identifier) is a structured value that identifies a resource. A deep link is usually a URI received from Android or iOS. This chapter assumes you know basic Dart syntax, Flutter navigation, and the main parts of a URL.
Consider this link:
https://example.com/products/42?ref=email#details
Each part has a separate meaning:
| Part | Example | Typical use |
|---|---|---|
| Scheme | `https` | Chooses the communication or link type |
| Host | `example.com` | Identifies the domain |
| Path | `/products/42` | Identifies a resource or route |
| Query | `ref=email` | Provides optional parameters |
| Fragment | `details` | Identifies a section within the resource |
The fragment follows #. It may be useful on the web, but mobile routing usually focuses on the path and query.
Parse a URI with Dart
Dart's Uri class parses the string into named properties. This is safer than splitting the string yourself because it handles delimiters and percent-encoded characters according to URI rules.
void main() {
final uri = Uri.parse(
'https://example.com/products/42?ref=email&sort=price#details',
);
print(uri.scheme); // https
print(uri.host); // example.com
print(uri.path); // /products/42
print(uri.pathSegments); // [products, 42]
print(uri.query); // ref=email&sort=price
print(uri.queryParameters['ref']); // email
print(uri.queryParameters['sort']); // price
print(uri.fragment); // details
}The pathSegments property is especially useful for routing. In this example, the first segment is products, and the second segment is the product ID, 42.
A query is a set of optional key-value pairs after ?. Use queryParameters when each key has one meaningful value. If a key can occur more than once, use queryParametersAll:
void main() {
final uri = Uri.parse('https://example.com/search?tag=flutter&tag=dart');
print(uri.queryParametersAll['tag']); // [flutter, dart]
}Dart decodes percent-encoded values when it exposes parsed parameters. For example, %20 represents a space. Do not assume that every incoming URI has the fields your route expects. A relative URI can have no scheme or host, and a missing query parameter produces null.
Relate URI parts to a route
A router commonly uses the path as the screen location and the query as optional state:
Avoid manually extracting values with fixed string positions. A path such as /products/42/ may include a trailing slash, and encoded characters can contain characters that would break simple splitting logic.
Common parsing mistakes
- Using
uri.hostto identify a custom-scheme route when the value is actually stored inuri.pathoruri.host, depending on the URI format. - Calling
int.parseon a path segment without handling invalid input. - Assuming query parameters have one fixed order.
- Forgetting that parameter values can be absent or repeated.
- Comparing encoded text instead of parsed values.
Exercise
For myapp://products/42?ref=push, what are the scheme, host, path segments, and value of ref?
Show answer
The scheme is myapp, the host is products, the path segments are [42], and ref is push.