Dart Tutorial

Dart Tutorial Single-Page Application Architecture Dart Features Dart Installation Guide Dart Basic Program Dart Syntax Dart Keywords Dart Variables Dart Comments Dart Standard Input Output Dart Important Concepts

Data Types

Built-in Data Types Numbers Strings Booleans Lists Sets Maps Runes and Graphemes Symbols Enumerations Constants Queues

Other data types

Objects Future and stream Iterable Miscellaneous types

OPERATORS

Precedence and associativity Arithmetic operators Equality and Relational operators Type Test Operators Assignment Operators Logical Operators Bitwise and Shift Operators Miscellaneous operators

Control Flow Statements

Introduction If statement If-else statement If-else-if statement Loops Switch and case Dart Break And Continue Assert In Dart

FUNCTIONS

Dart function Types of Functions Anonymous function main( ) function Lexical scope and closure Recursion Common Collection Methods

Object Oriented Concepts

Dart Object-Oriented Concepts Dart Classes Dart Constructors Dart This Keyword Dart Super Keyword Static Members Method Overriding Dart Interfaces Inheritance Dart Abstract Classes Dart Builder Classes Dart Callable Classes

Dart Type System

Dart Type System Dart Soundness Dart Type Inference

MISCELLANEOUS

Dart Isolates Dart Typedef Dart Metadata Dart Packages Dart Generics Dart Generators Dart Concurrency Dart Unit Testing Dart Html Dom Dart URIs Dart Extends, With and Implements Keywords Dart Optional Parameters Rust Vs Dart C++ vs Dart Golang Vs Dart Dart Basics Exception Handling

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.