Dart Uri host
Syntax & Examples
Uri.host property
The `host` property in Dart's Uri class returns the host part of the authority component.
Syntax of Uri.host
The syntax of Uri.host property is:
String host
This host property of Uri returns the host part of the authority component.
Return Type
Uri.host returns value of type String
.
✐ Examples
1 URI with authority and host
In this example,
- We create a Uri object
uri1
by parsing the string 'http://example.com/path'. - We use the
host
property to get the host part of the URI's authority component. - We then print the result to standard output.
Dart Program
void main() {
Uri uri1 = Uri.parse('http://example.com/path');
String host1 = uri1.host;
print('Host of URI 1: $host1');
}
Output
Host of URI 1: example.com
2 URI without authority
In this example,
- We create a Uri object
uri2
by parsing the string 'mailto:user@example.com'. - We use the
host
property, which returns an empty string as there is no authority component in the URI. - We then print the result to standard output.
Dart Program
void main() {
Uri uri2 = Uri.parse('mailto:user@example.com');
String host2 = uri2.host;
print('Host of URI 2: $host2');
}
Output
Host of URI 2:
3 URI without scheme
In this example,
- We create a Uri object
uri3
by parsing the string 'file:///path/to/file'. - We use the
host
property, which returns an empty string as there is no authority component in the URI. - We then print the result to standard output.
Dart Program
void main() {
Uri uri3 = Uri.parse('file:///path/to/file');
String host3 = uri3.host;
print('Host of URI 3: $host3');
}
Output
Host of URI 3:
Summary
In this Dart tutorial, we learned about host property of Uri: the syntax and few working examples with output and detailed explanation for each example.