×

Dart URIs

The Uri class supports encoding and decoding of strings with the help of functions to be used in URIs ( which may also be known as URLs ). These functions control characters that are different from URIs, such as ‘ & ’ and ‘ = ’. This section also analyses and identifies the components of the URI — host, port, scheme, and many more.

Encoding and decoding fully qualified URIs

The methods used to encode and decode the characters other than the one with special meaning in a URI are ‘ encodeFull( ) ’ and ‘ decodeFull( ) ’ method. The characters with special meaning in a URI are : / , : , & , #.

Example 1 :

void main( ) {  
var Url = 'https://example.org/api?foo=some message' ;
var enc_msg = Uri.encodeFull( Url ) ;
assert( enc_msg == 'https://example.org/api?foo=some%20message' ) ;
var dec_msg = Uri.decodeFull( enc_msg ) ;
print( Url == dec_msg ) ;
}

Output:

true

Encoding and decoding URI components:

Another important concept to understand is encoding and decoding all the characters in the string that have special meaning in a URI, including but not only limited to the characters like :  / , & , and : , use the ‘ encode Component( ) ’ and ‘ decode Component( ) ’ methods.

Example 2 :

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

Output :

 true

Parsing URIs:

We can use URI fields such as paths to get the parts of URI object or a URI string. The ‘parse( ) ’ static method is used to create a URI from a string.

Example 3 :

void main( ) {    
var uri =
    Uri.parse( 'https://example.org:8080/foo/bar#frag' ) ;
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

Properties:

  1. authority → String : The authority component. 
  2. data → UriData? : This property access the structure of a data: URI. 
  3. fragment → String : The fragment identifier component. 
  4. hasAbsolutePath → bool : This method checks whether the URI has an absolute path (starting with '/').
  5. hasAuthority → bool : This method checks whether the URI has an authority component.
  6. hasEmptyPath → bool : This method whether the URI has an empty path.
  7. hasFragment → bool : This method checks whether the URI has a fragment part.
  8. hashCode → int : This method returns a hash code computed as toString().hashCode. 
  9. hasPort → bool : This method whether the URI has an explicit port. 
  10. hasQuery → bool : This method whether the URI has a query part.
  11. hasScheme → bool : This method whether the URI has a scheme component.
  12. host → String : This property depicts the host part of the authority component. 
  13. isAbsolute → bool : This method checks whether the URI is absolute.
  14. origin → String : This method returns the origin of the URI in the form scheme://host:port for the schemes http and https. 
  15. path → String : The path component.
  16. pathSegments → List<String> : This property splits the URI path into its segments. 
  17. port → int : This property points to the port part of the authority component. 
  18. query → String : The query component. 
  19. queryParameters → Map<String, String> : This poperty splits the URI query into the specfied map with respect to the FORM post's rules specified in the HTML 4.01 specification section 17.13.4. 
  20. queryParametersAll → Map<String, List<String>> : This method returns the URI query split into a map according to the rules specified for FORM post in the HTML 4.01 specification section 17.13.4. 
  21. runtimeType → Type : This property refers to the representation of the runtime type of the object.
  22. scheme → String : This property refers to the scheme component of the URI. 
  23. userInfo → String : This property points to the user info part of the authority component. 

Constructors :

  1. Uri( { String? scheme, String? userInfo, String? host, int? port, String? path, Iterable<String>? pathSegments, String? query, Map<String, dynamic>? queryParameters, String? Fragment } ) : This method creates a new URI from its components. 
  2. Uri.dataFromBytes( List<int> bytes, {String mimeType = "application/octet-stream", Map<String, String>? parameters, bool percentEncoded = false } ) : This method creates a data: URI containing an encoding of bytes. 
  3. Uri.dataFromString(String content, { String? mimeType, Encoding? encoding, Map<String, String>? parameters, bool base64 = false } ) : This method creates a data: URI containing the content string. 
  4. Uri.directory( String path, { bool? windows } ) : Like Uri.file except that a non-empty URI path ends in a slash. 
  5. Uri.file( String path, { bool? windows } ) : This method creates a new file URI from an absolute or relative file path. 
  6. Uri.http( String authority, String unencodedPath, [ Map<String, dynamic>? queryParameters ] ) : This method creates a new http URI from authority, path and query. 
  7. Uri.https( String authority, String unencodedPath, [Map<String, dynamic>? queryParameters ] ) : This constructor creates a new https URI from authority, path and query. 

Methods

  1. isScheme( String scheme ) → bool : This method checks whether the scheme of this Uri is scheme. 
  2. normalizePath( ) → Uri : This method returns a URI where the path has been normalized. 
  3. noSuchMethod( Invocation invocation ) → dynamic : This method is invoked when a non-existent method or property is accessed. 
  4. removeFragment( ) → Uri : This method creates a Uri that differs from this only in not having a fragment. 
  5. replace( { String? scheme, String? userInfo, String? host, int? port, String? path, Iterable<String>? pathSegments, String? query, Map<String, dynamic>? queryParameters, String? Fragment } ) → Uri : This method creates a new Uri based on this one, but with some parts replaced. 
  6. resolve( String reference ) → Uri : This method resolve reference as an URI relative to this. 
  7. resolveUri( Uri reference ) → Uri : This method resolve reference as an URI relative to this. 
  8. toFilePath( { bool? windows } ) → String : This method creates a file path from a file URI. 
  9. toString( ) → String : The normalized string representation of the URI.

Operators :

  1. operator == ( Object other ) → bool : This operator checks whether a URI is equal to another URI with the same normalized representation.

Static Properties

  1. base → Uri : The natural base URI for the current platform. 

Static Methods :

  1. decodeComponent( String encodedComponent ) → String : This method decodes the percent-encoding in encodedComponent.
  2. decodeFull( String uri ) → String : This method decodes the percent-encoding in uri.
  3. decodeQueryComponent( String encodedComponent, { Encoding encoding = utf8 } ) → String : This method decodes the percent-encoding in encodedComponent, converting pluses to spaces.
  4. encodeComponent( String component ) → String : This method encodes the string component using percent-encoding to make it safe for literal use as a URI component.
  5. encodeFull( String uri ) → String : This method encodes the string uri using percent-encoding to make it safe for literal use as a full URI.
  6. encodeQueryComponent( String component, { Encoding encoding = utf8 } ) → String : This method encodes the string component according to the HTML 4.01 rules for encoding the posting of a HTML form as a query string component.
  7. parse( String uri, [ int start = 0, int? end ] ) → Uri : This method creates a new Uri object by parsing a URI string.
  8. parseIPv4Address( String host ) → List< int > : This method parses the host as an IP version 4 ( IPv4 ) address, returning the address as a list of 4 bytes in network byte order ( big endian ).
  9. parseIPv6Address( String host, [ int start = 0, int? end ] ) → List< int > : This method parses the host as an IP version 6 ( IPv6 ) address.
  10. splitQueryString( String query, { Encoding encoding = utf8 } ) → Map< String, String > : This method splits the query into a map according to the rules specified for FORM post in the HTML 4.01 specification section 17.13.4.
  11. tryParse( String uri, [ int start = 0, int? end ] ) → Uri? : This method creates a new Uri object by parsing a URI string.

Related Topics

Dart - Control Flow Statements

The control flow statements are used to define the control on the flow of the program. Dart programs are executed in sequential order i.e., the order in which the programming...

1 minute read.

Dart this Keyword

The ' this ' keyword, similar to that in Java and C#, refers to an object of the class using it. It points to the current object of the class,...

5 minutes read.

Dart Method Overriding

Before understanding method overriding, it is important to clear the concept of polymorphism. Polymorphism is derived from two Greek words 'Poly' which means many and 'morphs' which means many forms....

5 minutes read.

Dart super keyword

The ' super ' keyword is used to refer to the immediate parent class object of the currently in-use child class. Using this keyword, we can invoke the superclass (...

6 minutes read.

Dart Standard Input & Output

Standard Input in Dart (stdin): The standard input stream reads data both synchronously and asynchronously from the keyboard.  In Dart programming language, .readLineSync( ) function is used to accept input from the...

3 minutes read.

Dart Type Test Operators

Type Test Operators in Dart These operators are used to check the types of expressions at runtime. Dart is a typed language, and we often want to assert that a value...

1 minute read.

Unit Testing in Dart

Typing testing ensures that your app keeps working as you add more features or change existing functionality. Unit testing helps to confirm the behavior of a single function, method, or...

4 minutes read.

Dart Break and Continue

Loop statements are used to change the normal sequence of flow of the program. Dart supports two types of loop control statements: Break StatementContinue Statement Break Statement : The ‘ break ’ statement is...

4 minutes read.

Dart Optional Parameters

Dart functions can have optional parameters with default values. When we create a function, we can specify parameters that calling code can provide; but if the calling code chooses not...

2 minutes read.

Dart Isolates

Dart successfully supports asynchronous programming which runs our program without any hindrance. This asynchronous programming is used to achieve concurrency. Dart supports concurrent programming with features such as ‘ async-await...

9 minutes read.

Dart Static Members

Static Members in Dart Static members are the members of the class declared using the 'static' keyword. the static members have the following characteristics :  All the objects of the class having...

3 minutes read.

Dart Iterable

Iterable is a collection of elements that are accessed sequentially. These elements are accessible using the iterator getter and stepping through the values using this getter. Let us understand the setting...

6 minutes read.

Dart Queues

A queue is a user-defined collection of data. It is based on the FIFO principle ( First In First Out ) which implies that the element to be inserted first,...

3 minutes read.

Dart Types of Functions

Functions are the set of statements intended to perform a specific task. They accept some input from the user, perform the specified computation and generate the output. They are very...

4 minutes read.

Dart HTML DOM

All web pages can be viewed as objects and exist within the browser window. We can access a web page using a web browser and it needs to be connected...

3 minutes read.

Dart Generics

Dart Generics is similar to Dart collections, which are used to store homogeneous data. As we have discussed in Dart's features, it is a language with types being optional. By default,...

4 minutes read.

Dart Variables

Variables are used to store the value of a particular data type referred to in the whole program by a name or identifier. They refer to a memory location as...

5 minutes read.

Dart Basic Program

Dart provides various convenient platforms to code and compile the program in Dart language. Some of them are: 1. Using IDE = Visual Studio code is a text editor with IDE....

3 minutes read.

Dart Bitwise and Shift Operators

The Bitwise operators are the operators that perform different operations bit by bit on the value of the two operands. List of Bitwise Operators in Dart Sr. Operators Description 1. & ( Binary AND ) It returns...

2 minutes read.

Interfaces in Dart

An interface in Dart refers to the syntax or blueprint that any class must adhere to. It basically defines the array of methods available on the object. It provides the...

3 minutes read.