E.Benedos
E.Benedos

Reputation: 1777

Http get query parameters from different lists

I need to make an HTTP GET request to retrieve some filtered data. My API accept 3 array of parameters and in my code I have 3 different lists of parameters. Is it possible to create a Map that contains parameters with same key and different values from 3 lists of int?

Future<List<Tour>> fetchFilteredTours(List<int> bikeTypeIds,
      final List<int> difficultyIds, final List<int> locationIds) async {
    final Map<String, String> qryParams = {
    };

    //How to create my qryParams with all arrays values?
    //Something like
    qryParams.add(Map.fromIterable(bikeTypeIds, key: (e) => 'bikeTypeId', value: (e) => e));

    final response = await http
        .get(Uri.http('myDomain', '/api/search/tours', qryParams));

    return parseResponse(response);
  }

The desired result is {{protocol}}://{{url}}/api/search/tours?bikeTypeId=1&bikeTypeId=2&difficultyId=1&locationId=1

Upvotes: 0

Views: 676

Answers (2)

Kahou
Kahou

Reputation: 3488

  final keys = ['bikeTypeId', 'difficultyId', 'locationId'];

  final values = [bikeTypeIds, difficultyIds, locationIds].map(
    (ids) => ids.map((e) => e.toString()),
  );

Create a Map instance associating the given keys to values.

  final qryParams = Map.fromIterables(keys, values);

Create a new URI from its components.

  Uri(scheme: 'https', host: 'myDomain', path: 'api/search/tours', queryParameters: qryParams);

Upvotes: 1

Brian Gorman
Brian Gorman

Reputation: 824

Try qryParams.add(Map.fromIterable(bikeTypeIds, key: (e) => 'bikeTypeId', value: (e) => '$e'))

Which would provide the Map<String, String> that Uri.http requires. In your code you are trying to create a Map<String, int> ... Notice the '$e'

Upvotes: 0

Related Questions