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