Open In App

Dart – URIs

Last Updated : 30 Aug, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The Uri class supports functions to encode and decode strings for use in URIs (which you might know as URLs). These functions control characters that are unique for URIs, such as & and =. This class also parses and exposes the components of a URI—host, port, scheme, and so on.

Encoding and decoding fully qualified URIs

The encodeFull() and decodeFull() methods are used to encode and decode characters except those with special meaning in a URI (such as /, :, &, #).

Example: 

Dart




void main(){
    
var uri = 'https://example.org/api?foo=some message';
  
var encoded = Uri.encodeFull(uri);
assert(encoded ==
  
var decoded = Uri.decodeFull(encoded);
print(uri == decoded);
}


Output:

true

Encoding and decoding URI components

To encode and decode all of a string’s characters that have special meaning in a URI, including (but not limited to) /, &, and :, use the encodeComponent() and decodeComponent() methods.

Example:

Dart




void main(){
    
var uri = 'https://example.org/api?foo=some message';
  
var encoded = Uri.encodeComponent(uri);
assert(encoded ==
    'https%3A%2F%2Fexample.org%2Fapi%3Ffoo%3Dsome%20message');
  
var decoded = Uri.decodeComponent(encoded);
print(uri == decoded);
}


Output:

true

Parsing URIs

If you have a URI object or a URI string, you can get its parts using URI fields such as path. To create a URI from a string, use the parse() static method.

Example:

Dart




void main(){
    
var uri =
  
assert(uri.scheme == 'https');
assert(uri.host == 'example.org');
assert(uri.path == '/foo/bar');
assert(uri.fragment == 'frag');
print(uri.origin == 'https://example.org:8080');
}


Output:

true

Building URIs

You can  using the Uri() constructor is used to build up a URI from individual parts

Example:

Dart




void main(){
    
var uri = Uri(
    scheme: 'https',
    host: 'example.org',
    path: '/foo/bar',
    fragment: 'frag');
print(
    uri.toString() == 'https://example.org/foo/bar#frag');
}


Output:

true


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads