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

15,268

Solution 1

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

Solution 2

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);

Solution 3

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)
Share:
15,268
Jesse
Author by

Jesse

Updated on June 05, 2022

Comments

  • Jesse
    Jesse almost 2 years

    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.

  • Sisir
    Sisir about 3 years
    smart way. If no params then just Uri _uri = Uri.parse(_urlString)
  • Johan Witters
    Johan Witters almost 3 years
    Thanks! That's what I was looking for