dart:core
Uri class
A parsed URI, such as a URL.
To create a URI with specific components, use new Uri:
var httpsUri = Uri(
scheme: 'https',
host: 'dart.dev',
path: '/guides/libraries/library-tour',
fragment: 'numbers');
print(httpsUri); // https://dart.dev/guides/libraries/library-tour#numbers
httpsUri = Uri(
scheme: 'https',
host: 'example.com',
path: '/page/',
queryParameters: {'search': 'blue', 'limit': '10'});
print(httpsUri); // https://example.com/page/?search=blue&limit=10
final mailtoUri = Uri(
scheme: 'mailto',
path: '[email protected]',
queryParameters: {'subject': 'Example'});
print(mailtoUri); // mailto:[email protected]?subject=Example HTTP and HTTPS URI
To create a URI with https scheme, use Uri.https or Uri.http:
final httpsUri = Uri.https('example.com', 'api/fetch', {'limit': '10'});
print(httpsUri); // https://example.com/api/fetch?limit=10 File URI
To create a URI from file path, use Uri.file:
final fileUriUnix =
Uri.file(r'/home/myself/images/image.png', windows: false);
print(fileUriUnix); // file:///home/myself/images/image.png
final fileUriWindows =
Uri.file(r'C:\Users\myself\Documents\image.png', windows: true);
print(fileUriWindows); // file:///C:/Users/myself/Documents/image.png If the URI is not a file URI, calling this throws UnsupportedError.
Directory URI
Like Uri.file except that a non-empty URI path ends in a slash.
final fileDirectory =
Uri.directory('/home/myself/data/image', windows: false);
print(fileDirectory); // file:///home/myself/data/image/
final fileDirectoryWindows = Uri.directory('/data/images', windows: true);
print(fileDirectoryWindows); // file:///data/images/ URI from string
To create a URI from string, use Uri.parse or Uri.tryParse:
final uri = Uri.parse(
'https://dart.dev/guides/libraries/library-tour#utility-classes');
print(uri); // https://dart.dev
print(uri.isScheme('https')); // true
print(uri.origin); // https://dart.dev
print(uri.host); // dart.dev
print(uri.authority); // dart.dev
print(uri.port); // 443
print(uri.path); // guides/libraries/library-tour
print(uri.pathSegments); // [guides, libraries, library-tour]
print(uri.fragment); // utility-classes
print(uri.hasQuery); // false
print(uri.data); // null See also:
Constructors
- Uri({String? scheme, String? userInfo, String? host, int? port, String? path, Iterable<String>? pathSegments, String? query, Map<String, dynamic>? queryParameters, String? fragment})
factory
- Creates a new URI from its components.
- Uri.dataFromBytes(List<int> bytes, {String mimeType = "application/octet-stream", Map<String, String>? parameters, bool percentEncoded = false})
factory
- Creates a
data: URI containing an encoding of bytes. - Uri.dataFromString(String content, {String? mimeType, Encoding? encoding, Map<String, String>? parameters, bool base64 = false})
factory
- Creates a
data: URI containing the content string. - Uri.directory(String path, {bool? windows})
factory
- Like Uri.file except that a non-empty URI path ends in a slash.
- Uri.file(String path, {bool? windows})
factory
- Creates a new file URI from an absolute or relative file path.
- Uri.http(String authority, [String unencodedPath, Map<String, dynamic>? queryParameters])
factory
- Creates a new
http URI from authority, path and query. - Uri.https(String authority, [String unencodedPath, Map<String, dynamic>? queryParameters])
factory
- Creates a new
https URI from authority, path and query.
Properties
- authority → String
read-only
- The authority component.
- data → UriData?
read-only
- Access the structure of a
data: URI. - fragment → String
read-only
- The fragment identifier component.
- hasAbsolutePath → bool
read-only
- Whether the URI has an absolute path (starting with '/').
- hasAuthority → bool
read-only
- Whether the URI has an authority component.
- hasEmptyPath → bool
read-only
- Whether the URI has an empty path.
- hasFragment → bool
read-only
- Whether the URI has a fragment part.
- hashCode → int
read-only, override
- Returns a hash code computed as
toString().hashCode. - hasPort → bool
read-only
- Whether the URI has an explicit port.
- hasQuery → bool
read-only
- Whether the URI has a query part.
- hasScheme → bool
read-only
- Whether the URI has a scheme component.
- host → String
read-only
- The host part of the authority component.
- isAbsolute → bool
read-only
- Whether the URI is absolute.
- origin → String
read-only
- Returns the origin of the URI in the form scheme://host:port for the schemes http and https.
- path → String
read-only
- The path component.
- pathSegments → List<String>
read-only
- The URI path split into its segments.
- port → int
read-only
- The port part of the authority component.
- query → String
read-only
- The query component.
- queryParameters → Map<String, String>
read-only
- 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.
- queryParametersAll → Map<String, List<String>>
read-only
- 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
read-only, inherited
- A representation of the runtime type of the object.
- scheme → String
read-only
- The scheme component of the URI.
- userInfo → String
read-only
- The user info part of the authority component.
Methods
- isScheme(String scheme) → bool
- Whether the scheme of this Uri is
scheme. - normalizePath() → Uri
- Returns a URI where the path has been normalized.
- noSuchMethod(Invocation invocation) → dynamic
inherited
- Invoked when a non-existent method or property is accessed.
- removeFragment() → Uri
- 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
- Creates a new
Uri based on this one, but with some parts replaced. - resolve(String reference) → Uri
- Resolve
reference as an URI relative to this. - resolveUri(Uri reference) → Uri
- Resolve
reference as a URI relative to this. - toFilePath({bool? windows}) → String
- Creates a file path from a file URI.
- toString() → String
override
- The normalized string representation of the URI.
Operators
- operator ==(Object other) → bool
override
- A URI is equal to another URI with the same normalized representation.
Static Properties
- base → Uri
read-only
- The natural base URI for the current platform.
Static Methods
- decodeComponent(String encodedComponent) → String
- Decodes the percent-encoding in
encodedComponent. - decodeFull(String uri) → String
- Decodes the percent-encoding in
uri. - decodeQueryComponent(String encodedComponent, {Encoding encoding = utf8}) → String
- Decodes the percent-encoding in
encodedComponent, converting pluses to spaces. - encodeComponent(String component) → String
- Encode the string
component using percent-encoding to make it safe for literal use as a URI component. - encodeFull(String uri) → String
- 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
- 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
- Creates a new
Uri object by parsing a URI string. - parseIPv4Address(String host) → List<int>
- 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>
- Parses the
host as an IP version 6 (IPv6) address. - splitQueryString(String query, {Encoding encoding = utf8}) → Map<String, String>
- 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?
- Creates a new
Uri object by parsing a URI string.