Dart Uri hasAuthority
Syntax & Examples


Uri.hasAuthority property

The `hasAuthority` property in Dart's Uri class returns whether the URI has an authority component.


Syntax of Uri.hasAuthority

The syntax of Uri.hasAuthority property is:

 bool hasAuthority 

This hasAuthority property of Uri returns whether the URI has an authority component.

Return Type

Uri.hasAuthority returns value of type bool.



✐ Examples

1 URI with authority component

In this example,

  1. We create a Uri object uri1 by parsing the string 'http://example.com/path'.
  2. We use the hasAuthority property to check if the URI has an authority component.
  3. We then print the result to standard output.

Dart Program

void main() {
  Uri uri1 = Uri.parse('http://example.com/path');
  bool hasAuth1 = uri1.hasAuthority;
  print('URI 1 has authority: $hasAuth1');
}

Output

URI 1 has authority: true

2 URI without authority component

In this example,

  1. We create a Uri object uri2 by parsing the string 'mailto:user@example.com'.
  2. We use the hasAuthority property to check if the URI has an authority component.
  3. We then print the result to standard output.

Dart Program

void main() {
  Uri uri2 = Uri.parse('mailto:user@example.com');
  bool hasAuth2 = uri2.hasAuthority;
  print('URI 2 has authority: $hasAuth2');
}

Output

URI 2 has authority: false

Summary

In this Dart tutorial, we learned about hasAuthority property of Uri: the syntax and few working examples with output and detailed explanation for each example.