Dart Uri query
Syntax & Examples


Uri.query property

The `query` property in Dart's Uri class returns the encoded query component of the URI.


Syntax of Uri.query

The syntax of Uri.query property is:

 String query 

This query property of Uri returns the query component. The returned query is encoded. To get direct access to the decoded query use queryParameters.

Return Type

Uri.query returns value of type String.



✐ Examples

1 URI with query parameters

In this example,

  1. We create a Uri object uri1 by parsing the string 'http://example.com/path?param1=value1¶m2=value2'.
  2. We use the query property to get the encoded query component of the URI.
  3. We then print the result to standard output.

Dart Program

void main() {
  Uri uri1 = Uri.parse('http://example.com/path?param1=value1&param2=value2');
  String query1 = uri1.query;
  print('Query of URI 1: $query1');
}

Output

Query of URI 1: param1=value1&param2=value2

2 URI without query

In this example,

  1. We create a Uri object uri2 by parsing the string 'mailto:user@example.com'.
  2. We use the query property, which returns an empty string as there is no query component in the URI.
  3. We then print the result to standard output.

Dart Program

void main() {
  Uri uri2 = Uri.parse('mailto:user@example.com');
  String query2 = uri2.query;
  print('Query of URI 2: $query2');
}

Output

Query of URI 2: 

3 URI without scheme

In this example,

  1. We create a Uri object uri3 by parsing the string 'file:///path/to/file'.
  2. We use the query property, which returns an empty string as there is no scheme in the URI.
  3. We then print the result to standard output.

Dart Program

void main() {
  Uri uri3 = Uri.parse('file:///path/to/file');
  String query3 = uri3.query;
  print('Query of URI 3: $query3');
}

Output

Query of URI 3: 

Summary

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