×

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 Recursion

Recursion is one of the most important and interesting concepts in any programming language. It can be defined as a process in which a function calls itself directly or indirectly....

5 minutes read.

Dart Type inference

The task of interpreting the data types of fields, methods, local variables and most generic type arguments is handled by the static analyser. However, when not enough information is provided...

5 minutes read.

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 Inheritance

Inheritance is a promising concept of any programming language. It is a property by which a class inherits the property of another class. Moreover, the class deriving the properties of...

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 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 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 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.

Dart If-else-if statement

If - else - if statement enables to check the set of test expressions that evaluate to Boolean True or False, and based on this true or false the particular...

2 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 Maps

Map is an object-based data structure that associates keys and values that can be of any data type. Every key can occur only once, while the values can be used multiple...

5 minutes read.

Dart Keywords

Keywords are the reserved words of a programming language, and they have a special meaning that comes defined by the language. These cannot be used as an identifier, and all...

1 minute read.

Dart Objects

Apart from the built-in data types, Dart offers some other data types that have a special role to play. One of them is Objects.  Objects Objects are the foundation of the...

3 minutes read.

Dart If-else statement

In the previous section, we studied about if statements in detail. If – block is executed when the condition evaluates to true, else if the condition evaluates to false, then...

1 minute read.

Dart Syntax

In the previous tutorial, we coded our first program in Dart on various platforms understanding the basic functionality of each line of code.  Let us understand the syntax for Dart language...

4 minutes read.

Dart Loops

Looping refers to repeating or reusing the same lines of code repeatedly until a particular test condition evaluates to true. It is also known as iterating.  It is effectively used when...

4 minutes read.

Dart Single-Page Application Architecture

In a single page application architecture, the source code for a single web page loads the entire application. The responsibility of building the user interface and requesting data from the...

2 minutes read.

Abstract Classes in Dart

Abstract classes in Dart are those classes that only contain abstract methods  (methods that do not contain any implementation). These classes are specifically designed for the purpose of inheritance. We...

4 minutes read.

Dart Assignment Operator

Assignment operators are the operators that assign value to the variables. The value on the right-hand side is assigned to the variable on the left-hand side. We can also use...

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.