Dart Uri hasFragment
Syntax & Examples
Uri.hasFragment property
The `hasFragment` property in Dart's Uri class returns whether the URI has a fragment part.
Syntax of Uri.hasFragment
The syntax of Uri.hasFragment property is:
bool hasFragment
This hasFragment property of Uri returns whether the URI has a fragment part.
Return Type
Uri.hasFragment returns value of type bool
.
✐ Examples
1 URI with fragment part
In this example,
- We create a Uri object
uri1
by parsing the string 'http://example.com/path#fragment'. - We use the
hasFragment
property to check if the URI has a fragment part. - We then print the result to standard output.
Dart Program
void main() {
Uri uri1 = Uri.parse('http://example.com/path#fragment');
bool hasFrag1 = uri1.hasFragment;
print('URI 1 has fragment: $hasFrag1');
}
Output
URI 1 has fragment: true
2 URI without fragment part
In this example,
- We create a Uri object
uri2
by parsing the string 'mailto:user@example.com'. - We use the
hasFragment
property to check if the URI has a fragment part. - We then print the result to standard output.
Dart Program
void main() {
Uri uri2 = Uri.parse('mailto:user@example.com');
bool hasFrag2 = uri2.hasFragment;
print('URI 2 has fragment: $hasFrag2');
}
Output
URI 2 has fragment: false
3 URI without scheme
In this example,
- We create a Uri object
uri3
by parsing the string 'file:///path/to/file'. - We use the
hasFragment
property to check if the URI has a fragment part. - We then print the result to standard output.
Dart Program
void main() {
Uri uri3 = Uri.parse('file:///path/to/file');
bool hasFrag3 = uri3.hasFragment;
print('URI 3 has fragment: $hasFrag3');
}
Output
URI 3 has fragment: false
Summary
In this Dart tutorial, we learned about hasFragment property of Uri: the syntax and few working examples with output and detailed explanation for each example.