getMany method

Future<List<Map<String, dynamic>>> getMany({
  1. required String accessToken,
  2. required String companyId,
  3. String location = 'Item',
  4. String? conditions,
})

Gets the requested data group from Quickbooks Api with the given accessToken and companyId. Parse it in a List<Map<String, dynamic>> with the location given with the searched data in it.

Implementation

Future<List<Map<String, dynamic>>> getMany(
    {required String accessToken,
    required String companyId,
    String location = 'Item',
    String? conditions}) async {
  var query = buildQuery(conditions: conditions);
  return await http.get(
      Uri.https(
        _baseEndpoint,
        '$_companyEndpoint$companyId/query',
        {
          'minorversion': '65',
          'query': query,
        },
      ),
      headers: {
        'Accept': 'application/json',
        'content-type': 'text/plain',
        'Authorization': 'Bearer $accessToken'
      }).then((response) async {
    switch (response.statusCode) {
      case 200:
        Map<String, dynamic> body = jsonDecode(response.body);
        List<Map<String, dynamic>> items = [];
        try {
          for (var value in (body["QueryResponse"][location] ?? []) as List) {
            try {
              items.add(value as Map<String, dynamic>);
            } catch (_) {}
          }
        } catch (_) {
          return [];
        }
        return items;
      default:
        throw AlfredException(500, 'Quickbooks error: ${response.body}');
    }
  });
}