Dart Uri normalizePath()
Syntax & Examples


Uri.normalizePath() method

The `normalizePath` method in Dart's Uri class returns a URI where the path has been normalized.


Syntax of Uri.normalizePath()

The syntax of Uri.normalizePath() method is:

 Uri normalizePath() 

This normalizePath() method of Uri returns a URI where the path has been normalized.

Return Type

Uri.normalizePath() returns value of type Uri.



✐ Examples

1 URI with relative path components

In this example,

  1. We create a Uri object uri1 by parsing the string 'http://example.com/foo/../bar'.
  2. We use the normalizePath method to normalize the path.
  3. We then print the result to standard output.

Dart Program

void main() {
  Uri uri1 = Uri.parse('http://example.com/foo/../bar');
  Uri normalizedUri1 = uri1.normalizePath();
  print('Normalized URI 1: $normalizedUri1');
}

Output

Normalized URI 1: http://example.com/bar

2 File URI with redundant path components

In this example,

  1. We create a Uri object uri2 by parsing the string 'file:///home/user/documents/./report.txt'.
  2. We use the normalizePath method to normalize the path.
  3. We then print the result to standard output.

Dart Program

void main() {
  Uri uri2 = Uri.parse('file:///home/user/documents/./report.txt');
  Uri normalizedUri2 = uri2.normalizePath();
  print('Normalized URI 2: $normalizedUri2');
}

Output

Normalized URI 2: file:///home/user/documents/report.txt

3 HTTPS URI with trailing slash

In this example,

  1. We create a Uri object uri3 by parsing the string 'https://example.com/path/to/'.
  2. We use the normalizePath method, which does not alter the path as it's already normalized.
  3. We then print the result to standard output.

Dart Program

void main() {
  Uri uri3 = Uri.parse('https://example.com/path/to/');
  Uri normalizedUri3 = uri3.normalizePath();
  print('Normalized URI 3: $normalizedUri3');
}

Output

Normalized URI 3: https://example.com/path/to/

Summary

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