Dart Uri queryParameters
Syntax & Examples
Uri.queryParameters property
The `queryParameters` property in Dart's Uri class 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. Each key and value in the returned map has been decoded. If there is no query, the empty map is returned.
Syntax of Uri.queryParameters
The syntax of Uri.queryParameters property is:
Map<String, String> queryParameters
This queryParameters property of Uri returns the URI query split into a map according to the rules specified for FORM post in the HTML 4.01 specification. Each key and value in the returned map has been decoded. If there is no query the empty map is returned.
Return Type
Uri.queryParameters returns value of type Map<String, String>
.
✐ Examples
1 Getting query parameters from a URI with a query
In this example,
- We parse a URI with a query string.
- We access the `queryParameters` property of the URI.
- We print the decoded query parameters as a map to standard output.
Dart Program
void main() {
Uri uri1 = Uri.parse('https://example.com/page?param1=value1&param2=value2');
print(uri1.queryParameters); // {param1: value1, param2: value2}
}
Output
{param1: value1, param2: value2}
2 Getting query parameters from a URI without a query
In this example,
- We parse a URI without a query string.
- We access the `queryParameters` property of the URI.
- We print an empty map since there are no query parameters.
Dart Program
void main() {
Uri uri2 = Uri.parse('https://example.com/no_query');
print(uri2.queryParameters); // {}
}
Output
{}
Summary
In this Dart tutorial, we learned about queryParameters property of Uri: the syntax and few working examples with output and detailed explanation for each example.