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
Parameter | Optional/Required | Description |
---|---|---|
authority | required | The authority part of the URI, typically the domain name or IP address. |
unencodedPath | optional | The path part of the URI. It should not be URL-encoded. |
queryParameters | optional | A map of query parameters as key-value pairs. |
✐ Examples
1 Creating an HTTP URI with authority, path, and query parameters
In this example,
- We define a string variable `authority` representing the server name.
- We define a string variable `path` representing the path on the server.
- We create a map of query parameters.
- We use the `Uri.http` constructor to create an HTTP URI with the provided parameters.
- 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,
- We define a string variable `authority` representing the server name and port.
- We use the `Uri.http` constructor with only the authority parameter.
- 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.