I use dio in my Flutter app. I have get request. I need add int array to request param. This is an example:

api/my?param1=6&param2=5&exclude_id=[7364, 7365, 7366]

This is my code in dart:

 List<int> ids = [7364, 7365, 7366]
 String jsonIds = jsonEncode(ids); 
 final params = <String, dynamic>{
    'param1': 6,
    'param2': 5,
    "exclude_id": jsonIds
  };
 final response = await _dio.get('/api/my', queryParameters: params);

but it comes to the server as (from the text log):

   api/my?param1=25\u0026param2=6\u0026exclude_id=%5B7172%2C8%2C7150%2C7156%2C3848%2C7212%2C7168%2C15%2C24%2C4102%2C1032%5D

how do I add this parameter to the request correctly? any advice?


Solution 1: cameron1024

If you know that the server is able to handle the ints in the form: "1234,2345,3456", you can simply join them before you pass them to dio:

 List<int> ids = [7364, 7365, 7366]
 final params = <String, dynamic>{
    'param1': 6,
    'param2': 5,
    "exclude_id": ids.join(','),  // "7364,7365,7366"
  };
 final response = await _dio.get('/api/my', queryParameters: params);

Note, the correct answer depends on what format your server expects. If it is a 3rd part server, try looking at their docs.

Also, HTTP URLs cannot contain (among other things) whitespace, but the output of jsonEncode may include whitespace. Generally it is not common to see JSON-encoded data in URL parameters, it is much more common in request/response bodies