Flutter/Dart Uri not escaping colon ":" in URL

Viewed 19124

The following uri created:

final _url = "https://example.com/api/";
final _uri = Uri(path: _url, queryParameters: _params);

Results in http%3A//example.com/api/+{params...}

I have tried escaping by \: and other methods but no luck :/

This problem only happens when run through Uri, I was unable to find any resources online to resolve this issue.

3 Answers

So the best way to create an URI in Dart from a url string is using the Uri dart package with its static methods http or https

So you have to change your code as the following:

final _authority = "example.com";
final _path = "/api";
final _params = { "q" : "dart" };
final _uri =  Uri.https(_authority, _path, _params);

enter image description here

Replace:

final _url = "https://example.com/api/";
final _uri = Uri(path: _url, queryParameters: _params);

with:

final _url = "example.com/api/";
final _uri = Uri.https(path: _url, queryParameters: _params);

Your intention is

final String _url = "https://example.com/api/";
final Uri _uri = Uri
    .parse(_url)  // parse string
    .replace(queryParameters: _params);  // set params (no need manual escape, Uri do it itself)
Related