listInvoices method

Future<InvoiceResponse> listInvoices({
  1. required String locationId,
  2. String? cursor,
  3. int? limit,
  4. String? authToken,
})

Returns a list of invoices for a given location.

The response is paginated. If truncated, the response includes a cursor that you use in a subsequent request to retrieve the next set of invoices.

Implementation

Future<InvoiceResponse> listInvoices({
  required String locationId,
  String? cursor,
  int? limit,
  String? authToken,
}) async {

  limit ??= 200;
  authToken ??= authenticationService.getCachedToken()?.accessToken;

  Map<String, String> headers = {
    "Authorization": "Bearer ${authToken ?? ""}",
    'Content-Type': 'application/json; charset=UTF-8',
    'Accept': 'application/json',

  };

  var params = {
    "location_id": locationId,
    "limit": limit
  };

  if (cursor!= null) {
    params.putIfAbsent("cursor", () => cursor);
  }

  Uri endpoint = Uri.https(
      baseUrl, "/v2/invoices", params);

  //print (endpoint.toString());

  var response = await
  http.get(endpoint, headers: headers);

  if (response.statusCode == 200) {
    print (jsonDecode(response.body));
    return InvoiceResponse.fromJson(jsonDecode(response.body));
  }
  else {
    print (response.body);
    throw InvoiceException(statusCode: response.statusCode, message: InvoiceResponse.fromJson(jsonDecode(response.body)).errors?[0].detail?.toString());
  }
}