Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 784
Jesse Avatar asked Dec 17 '18 09:12

Jesse


3 Answers

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)
like image 75
theanurin Avatar answered Nov 18 '22 21:11

theanurin


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

like image 34
shadowsheep Avatar answered Nov 18 '22 20:11

shadowsheep


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);
like image 33
Nae Avatar answered Nov 18 '22 21:11

Nae