Dart Uri port
Syntax & Examples


Uri.port property

The `port` property in Dart's Uri class returns the port part of the authority component.


Syntax of Uri.port

The syntax of Uri.port property is:

 int port 

This port property of Uri returns the port part of the authority component.

Return Type

Uri.port returns value of type int.



✐ Examples

1 Getting the port from an HTTPS URI with a custom port

In this example,

  1. We parse an HTTPS URI with a custom port specified.
  2. We access the `port` property of the URI.
  3. We print the port to standard output.

Dart Program

void main() {
  Uri uri = Uri.parse('https://example.com:8080');
  print(uri.port); // 8080
}

Output

8080

2 Getting the default port for FTP URI

In this example,

  1. We parse an FTP URI without a custom port.
  2. We access the `port` property of the URI.
  3. We print the default FTP port (21) to standard output.

Dart Program

void main() {
  Uri uri = Uri.parse('ftp://example.com');
  print(uri.port); // 21 (default FTP port)
}

Output

21

3 Getting the default port for HTTPS URI

In this example,

  1. We parse an HTTPS URI without a custom port.
  2. We access the `port` property of the URI.
  3. We print the default HTTPS port (443) to standard output.

Dart Program

void main() {
  Uri uri = Uri.parse('https://example.com');
  print(uri.port); // 443 (default HTTPS port)
}

Output

443

Summary

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