getProducts static method

Future<Map<String, String>> getProducts({
  1. required String key,
  2. String? value,
  3. QueryType? queryType,
})

Returns all the products with that key.

The key of the returned map is the barcode, the value is the tag value.

Implementation

static Future<Map<String, String>> getProducts({
  required final String key,
  final String? value,
  final QueryType? queryType,
}) async {
  final Map<String, String> parameters = <String, String>{};
  /* TODO
  if (owner != null) {
    parameters['owner'] = owner;
  }
   */
  parameters['k'] = key;
  if (value != null) {
    parameters['v'] = value;
  }
  final Response response = await HttpHelper().doGetRequest(
    UriHelper.getFolksonomyUri(
      path: 'products',
      queryParameters: parameters,
      queryType: queryType,
    ),
    queryType: queryType,
  );
  _checkResponse(response);
  final Map<String, String> result = <String, String>{};
  final List<dynamic> json = jsonDecode(response.body) as List<dynamic>;
  for (var element in json) {
    final ProductList productList = ProductList.fromJson(element);
    if (productList.key != key) {
      throw Exception('Unexpected key: ${productList.key}');
    }
    result[productList.barcode] = productList.value;
  }
  return result;
}