Dart Uri path
Syntax & Examples
Uri.path property
The `path` property in Dart's Uri class returns the path component of the URI.
Syntax of Uri.path
The syntax of Uri.path property is:
String path
This path property of Uri returns the path component.
Return Type
Uri.path returns value of type String
.
✐ Examples
1 Getting the path from an HTTP URI
In this example,
- We parse an HTTP URI with a path component.
- We access the `path` property of the URI.
- We print the path to standard output.
Dart Program
void main() {
Uri uri = Uri.parse('https://example.com/path/to/resource');
print(uri.path); // /path/to/resource
}
Output
/path/to/resource
2 Getting the path from an HTTP URI without a path
In this example,
- We parse an HTTP URI without a path component.
- We access the `path` property of the URI.
- We print the path, which is an empty string since there is no path in the URI.
Dart Program
void main() {
Uri uri = Uri.parse('https://example.com');
print(uri.path); // '' (empty string)
}
Output
3 Getting the path from a file URI
In this example,
- We parse a file URI with a path component.
- We access the `path` property of the URI.
- We print the path to standard output.
Dart Program
void main() {
Uri uri = Uri.parse('file:///path/to/file.txt');
print(uri.path); // /path/to/file.txt
}
Output
/path/to/file.txt
Summary
In this Dart tutorial, we learned about path property of Uri: the syntax and few working examples with output and detailed explanation for each example.