Dart Uri hasPort
Syntax & Examples


Uri.hasPort property

The `hasPort` property in Dart's Uri class returns whether the URI has an explicit port.


Syntax of Uri.hasPort

The syntax of Uri.hasPort property is:

 bool hasPort 

This hasPort property of Uri returns whether the URI has an explicit port.

Return Type

Uri.hasPort returns value of type bool.



✐ Examples

1 Checking an HTTP URI without an explicit port

In this example,

  1. We parse an HTTP URI without an explicit port.
  2. We access the `hasPort` property of the URI.
  3. We print the result, which is false since the URI does not specify a port.

Dart Program

void main() {
  Uri uri1 = Uri.parse('https://example.com');
  print(uri1.hasPort); // false
}

Output

false

2 Checking an HTTP URI with an explicit port

In this example,

  1. We parse an HTTP URI with an explicit port.
  2. We access the `hasPort` property of the URI.
  3. We print the result, which is true since the URI specifies a port.

Dart Program

void main() {
  Uri uri2 = Uri.parse('https://example.com:8080');
  print(uri2.hasPort); // true
}

Output

true

3 Checking an FTP URI without an explicit port

In this example,

  1. We parse an FTP URI without an explicit port.
  2. We access the `hasPort` property of the URI.
  3. We print the result, which is false since the URI does not specify a port.

Dart Program

void main() {
  Uri uri3 = Uri.parse('ftp://example.com/file.txt');
  print(uri3.hasPort); // false
}

Output

false

Summary

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