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:
- authority → String : The authority component.
- data → UriData? : This property access the structure of a data: URI.
- fragment → String : The fragment identifier component.
- hasAbsolutePath → bool : This method checks whether the URI has an absolute path (starting with '/').
- hasAuthority → bool : This method checks whether the URI has an authority component.
- hasEmptyPath → bool : This method whether the URI has an empty path.
- hasFragment → bool : This method checks whether the URI has a fragment part.
- hashCode → int : This method returns a hash code computed as toString().hashCode.
- hasPort → bool : This method whether the URI has an explicit port.
- hasQuery → bool : This method whether the URI has a query part.
- hasScheme → bool : This method whether the URI has a scheme component.
- host → String : This property depicts the host part of the authority component.
- isAbsolute → bool : This method checks whether the URI is absolute.
- origin → String : This method returns the origin of the URI in the form scheme://host:port for the schemes http and https.
- path → String : The path component.
- pathSegments → List<String> : This property splits the URI path into its segments.
- port → int : This property points to the port part of the authority component.
- query → String : The query component.
- 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.
- 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.
- runtimeType → Type : This property refers to the representation of the runtime type of the object.
- scheme → String : This property refers to the scheme component of the URI.
- userInfo → String : This property points to the user info part of the authority component.
Constructors :
- 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.
- 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.
- 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.
- Uri.directory( String path, { bool? windows } ) : Like Uri.file except that a non-empty URI path ends in a slash.
- Uri.file( String path, { bool? windows } ) : This method creates a new file URI from an absolute or relative file path.
- Uri.http( String authority, String unencodedPath, [ Map<String, dynamic>? queryParameters ] ) : This method creates a new http URI from authority, path and query.
- Uri.https( String authority, String unencodedPath, [Map<String, dynamic>? queryParameters ] ) : This constructor creates a new https URI from authority, path and query.
Methods
- isScheme( String scheme ) → bool : This method checks whether the scheme of this Uri is scheme.
- normalizePath( ) → Uri : This method returns a URI where the path has been normalized.
- noSuchMethod( Invocation invocation ) → dynamic : This method is invoked when a non-existent method or property is accessed.
- removeFragment( ) → Uri : This method creates a Uri that differs from this only in not having a fragment.
- 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.
- resolve( String reference ) → Uri : This method resolve reference as an URI relative to this.
- resolveUri( Uri reference ) → Uri : This method resolve reference as an URI relative to this.
- toFilePath( { bool? windows } ) → String : This method creates a file path from a file URI.
- toString( ) → String : The normalized string representation of the URI.
Operators :
- operator == ( Object other ) → bool : This operator checks whether a URI is equal to another URI with the same normalized representation.
Static Properties
- base → Uri : The natural base URI for the current platform.
Static Methods :
- decodeComponent( String encodedComponent ) → String : This method decodes the percent-encoding in encodedComponent.
- decodeFull( String uri ) → String : This method decodes the percent-encoding in uri.
- decodeQueryComponent( String encodedComponent, { Encoding encoding = utf8 } ) → String : This method decodes the percent-encoding in encodedComponent, converting pluses to spaces.
- encodeComponent( String component ) → String : This method encodes the string component using percent-encoding to make it safe for literal use as a URI component.
- encodeFull( String uri ) → String : This method encodes the string uri using percent-encoding to make it safe for literal use as a full URI.
- 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.
- parse( String uri, [ int start = 0, int? end ] ) → Uri : This method creates a new Uri object by parsing a URI string.
- 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 ).
- parseIPv6Address( String host, [ int start = 0, int? end ] ) → List< int > : This method parses the host as an IP version 6 ( IPv6 ) address.
- 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.
- tryParse( String uri, [ int start = 0, int? end ] ) → Uri? : This method creates a new Uri object by parsing a URI string.