2

Is there some function that adds the request params to an http request in way that you don't have to do it "manually"? For example, if I want to put "user": "x" as param of my request, in a way to achieve something like

http:test/testing?user=x

how can I do it?

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
Little Monkey
  • 5,395
  • 14
  • 45
  • 83

2 Answers2

12

You can't add params to a request, you need to add it to an URL you use for the request. The Uri class provides methods for that

var uri = Uri.parse('http://test/testing');
uri = uri.replace(query: 'user=x');
print(uri);

or

uri = uri.replace(queryParameters: <String, String>{'user': 'x'});

or

final uri = Uri.parse('http://test/testing').replace(query: 'user=x');
Grae
  • 55
  • 1
  • 6
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
1

It seems like a more direct way to do it is to use Uri.http.

// http://example.org/path?q=dart.
Uri.http("example.org", "/path", { "q" : "dart" });

Notes:

  • The query parameters are { "q" : "dart" }.
  • Uri.https() works the same way.
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393