Dart Uri.http()
Syntax & Examples


Uri.http constructor

The `Uri.http` constructor in Dart creates a new `http` URI from an authority, path, and optional query parameters.


Syntax of Uri.http

The syntax of Uri.Uri.http constructor is:

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

This Uri.http constructor of Uri creates a new http 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 HTTP URI with authority, path, and query parameters

In this example,

  1. We define a string variable `authority` representing the server name.
  2. We define a string variable `path` representing the path on the server.
  3. We create a map of query parameters.
  4. We use the `Uri.http` constructor to create an HTTP URI with the provided parameters.
  5. We print the URI as a string.

Dart Program

void main() {
  String authority = 'example.com';
  String path = '/api/data';
  Map<String, String> queryParameters = {'page': '1', 'limit': '10'};
  Uri uri = Uri.http(authority, path, queryParameters);
  print(uri.toString());
}

Output

http://example.com/api/data?page=1&limit=10

2 Creating a basic HTTP URI with only authority

In this example,

  1. We define a string variable `authority` representing the server name and port.
  2. We use the `Uri.http` constructor with only the authority parameter.
  3. We print the URI as a string.

Dart Program

void main() {
  String authority = 'localhost:8080';
  Uri uri = Uri.http(authority);
  print(uri.toString());
}

Output

http://localhost:8080/

Summary

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