Dart Uri resolveUri()
Syntax & Examples


resolveUri() method

The `resolveUri` method in Dart's Uri class resolves the provided URI reference as an URI relative to the current URI.


Syntax of resolveUri()

The syntax of Uri.resolveUri() method is:

 Uri resolveUri(Uri reference) 

This resolveUri() method of Uri resolve reference as an URI relative to this.

Parameters

ParameterOptional/RequiredDescription
referencerequiredThe URI reference to be resolved relative to the current URI.

Return Type

Uri.resolveUri() returns value of type Uri.



✐ Examples

1 Resolving a relative path

In this example,

  1. We create a base Uri object baseUri with the value 'https://example.com/path/'.
  2. We create a reference Uri object referenceUri with the value 'doc.html'.
  3. We use the resolveUri method to resolve the reference URI relative to the base URI.
  4. We then print the result to standard output.

Dart Program

void main() {
  Uri baseUri = Uri.parse('https://example.com/path/');
  Uri referenceUri = Uri.parse('doc.html');
  Uri resolvedUri = baseUri.resolveUri(referenceUri);
  print('Resolved URI: $resolvedUri');
}

Output

Resolved URI: https://example.com/path/doc.html

2 Resolving a relative path with parent directory navigation

In this example,

  1. We create a base Uri object baseUri with the value 'https://example.com/path/file.txt'.
  2. We create a reference Uri object referenceUri with the value '../images/image.jpg'.
  3. We use the resolveUri method to resolve the reference URI relative to the base URI.
  4. We then print the result to standard output.

Dart Program

void main() {
  Uri baseUri = Uri.parse('https://example.com/path/file.txt');
  Uri referenceUri = Uri.parse('../images/image.jpg');
  Uri resolvedUri = baseUri.resolveUri(referenceUri);
  print('Resolved URI: $resolvedUri');
}

Output

Resolved URI: https://example.com/images/image.jpg

Summary

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