1

Is there any way to conveniently pass both parameters/queries as a Map<String,Dynamic> itself along with headers into the get request in flutter. As of now, I'm using http package and can only do it with url string manipulations as follows:

var headers={
    'accept': 'application/json',
    'Accept-Language': 'en_US',
    'user-agent': 'Mozilla/5....'
    };
final response = await http.get(Uri.parse(url+'?district_id=$district_id'+'&date=$date'), headers:headers);

the http.get only accepts url and headers, unlike in python get where payload dictionaries can be used as parameters. I want achieve it simply, like:

final response = await http.get(Uri.parse(url), params:params, headers:headers);

or any other method without string manipulation.

Update: I tried using Uri constructor. But it throws error. The Map I need to pass is

{'district_id':298, 'date':'10-06-2021'}; \\has int and String as values.

Error:

type 'int' is not a subtype of type 'Iterable'

As of now, I could only make it work by changing the int 298 to String '298'.

  • 1
    see https://api.flutter.dev/flutter/dart-core/Uri-class.html#constructors – pskink Jun 10 '21 at 06:10
  • @pskink thanks. That means I have to create the Uri object with the params Map before passing into get, right? –  Jun 10 '21 at 06:13
  • 1
    it means that you dont have to use `Uri.parse` static method to get your final `Uri` - you can use one of seven existing `Uri` constructors – pskink Jun 10 '21 at 06:15
  • I tried the Uri.https constructor. But there is still a problem with using this method. The Map I need to pass has both int and string as values. Hence, dart raises error "type 'int' is not a subtype of type 'Iterable'". How do I pass this Map directly? I was able to do this with url string manipulation. –  Jun 10 '21 at 16:43
  • and? `stringParams` work now? `stringParams = params.map((k, v) => MapEntry(k, v.toString()));` – pskink Jun 10 '21 at 17:32
  • Yes. the request is succuessful now. It works and so it is certain that Uri cannot take int type as a value for key. I guessed this was the problem. But what if the case is that web server only recognises an int value for that key. I just wanted to see if there is any way to send integer types –  Jun 10 '21 at 17:40
  • what do you mean by "int"? it is encoded in url like `http:/www.foo.com/foo/bar?someIntParam=100` so `100` here is an int or string? of course it is a string and server decodes that string to int value – pskink Jun 10 '21 at 17:52
  • Ok, now I got it. Thanks for the clarification. I was under the impression that get request would use quotes in order for the receiving API to differentiate the type: whether a '100' received is a String, from a 100 recieved as Int. I was thinking of a web model which auto-infers the type upon receipt and then I would have to sent it as 100 or '100' as per need. –  Jun 11 '21 at 05:48

1 Answers1

0

You can use dio.request which accepts a Map<String, dynamic> object called queryParameters.

dio.request(path,
    options: Options(method: 'GET', headers: headers),
    queryParameters: {'district_id': districtId},
)
Amir_P
  • 8,322
  • 5
  • 43
  • 92
  • does it take as queryParameters? –  Jun 10 '21 at 07:10
  • 1
    Yes it takes a `Map` @VaisakMohan – Amir_P Jun 10 '21 at 07:16
  • The Map I want to pass has both int and String as value types. Hence, there is an error:type 'int' is not a subtype of type 'Iterable'. Do you know how to deal with this? –  Jun 10 '21 at 16:44
  • https://pastebin.com/ftmCT5ta Here it is. The method used also takes Map as query, so you might know the reason from your wxperience with dio –  Jun 10 '21 at 17:03
  • Have you tried what I suggested? You're not using `dio.request` in that code. @VaisakMohan – Amir_P Jun 10 '21 at 17:05
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/233627/discussion-between-vaisak-mohan-and-amir-p). –  Jun 10 '21 at 17:20