Dart Uri resolve()
Syntax & Examples


Uri.resolve() method

The `resolve` method in Dart resolves the reference as a URI relative to the current URI.


Syntax of Uri.resolve()

The syntax of Uri.resolve() method is:

 Uri resolve(String reference) 

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

Parameters

ParameterOptional/RequiredDescription
referencerequiredThe URI reference to resolve relative to the current URI.

Return Type

Uri.resolve() returns value of type Uri.



✐ Examples

1 Resolving a relative path

In this example,

  1. We create a base URI using `Uri.parse()`.
  2. We use the `resolve()` method to resolve a relative path like 'page.html'.
  3. We then print the resolved URI.

Dart Program

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

Output

Resolved URI: https://example.com/page.html

2 Resolving an absolute path

In this example,

  1. We create a base URI using `Uri.parse()`.
  2. We use the `resolve()` method to resolve an absolute path '/path/to/page.html'.
  3. We then print the resolved URI.

Dart Program

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

Output

Resolved URI: https://example.com/path/to/page.html

3 Resolving with another domain

In this example,

  1. We create a base URI using `Uri.parse()`.
  2. We use the `resolve()` method to resolve a URI from another domain 'https://anotherdomain.com/page.html'.
  3. We then print the resolved URI.

Dart Program

void main() {
  Uri baseUri = Uri.parse('https://example.com');
  Uri resolvedUri = baseUri.resolve('https://anotherdomain.com/page.html');
  print('Resolved URI: $resolvedUri');
}

Output

Resolved URI: https://anotherdomain.com/page.html

Summary

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