Dart Uri.https()
Syntax & Examples


Uri.https constructor

The `Uri.https` constructor in Dart is used to create a new `https` URI from an authority, path, and optional query parameters.


Syntax of Uri.https

The syntax of Uri.Uri.https constructor is:

Uri.https(String authority, [ String unencodedPath, [ Map<String, String> queryParameters ])

This Uri.https constructor of Uri creates a new https URI from authority, path and query.

Parameters

ParameterOptional/RequiredDescription
authorityrequiredThe authority part of the URI, typically the domain name or IP address.
unencodedPathoptionalThe path part of the URI. It should not be URL-encoded.
queryParametersoptionalA map of query parameters as key-value pairs.


✐ Examples

1 Creating an HTTPS URI with authority, path, and query parameters

In this example,

  1. We define a `String` variable `authority` with the value 'example.com'.
  2. We define a `String` variable `path` with the value '/path/to/resource'.
  3. We define a `Map` variable `queryParameters` containing query parameters 'param1=value1' and 'param2=value2'.
  4. We use the `Uri.https` constructor to create an HTTPS URI with the specified authority, path, and query parameters.
  5. We print the URI to standard output.

Dart Program

void main() {
  String authority = 'example.com';
  String path = '/path/to/resource';
  Map<String, String> queryParameters = {'param1': 'value1', 'param2': 'value2'};
  Uri httpsUri = Uri.https(authority, path, queryParameters);
  print(httpsUri);
}

Output

https://example.com/path/to/resource?param1=value1&param2=value2

2 Creating an HTTPS URI with authority and path only

In this example,

  1. We define a `String` variable `authority` with the value 'sub.example.com'.
  2. We define a `String` variable `path` with the value '/file'.
  3. We use the `Uri.https` constructor to create an HTTPS URI with the specified authority and path.
  4. We print the URI to standard output.

Dart Program

void main() {
  String authority = 'sub.example.com';
  String path = '/file';
  Uri httpsUri = Uri.https(authority, path);
  print(httpsUri);
}

Output

https://sub.example.com/file

Summary

In this Dart tutorial, we learned about Uri.https constructor of Uri: the syntax and few working examples with output and detailed explanation for each example.