getCustomers method

Future<List<WooCustomer>> getCustomers({
  1. int? page,
  2. int? perPage,
  3. String? search,
  4. List<int>? exclude,
  5. List<int>? include,
  6. int? offset,
  7. String? order,
  8. String? orderBy,
  9. String? role,
})

Returns a list of all WooCustomer, with filter options.

Related endpoint: https://woocommerce.github.io/woocommerce-rest-api-docs/#customers

Implementation

Future<List<WooCustomer>> getCustomers(
    {int? page,
    int? perPage,
    String? search,
    List<int>? exclude,
    List<int>? include,
    int? offset,
    String? order,
    String? orderBy,
    //String email,
    String? role}) async {
  Map<String, dynamic> payload = {};

  ({
    'page': page, 'per_page': perPage, 'search': search,
    'exclude': exclude, 'include': include, 'offset': offset,
    'order': order, 'orderby': orderBy, //'email': email,
    'role': role,
  }).forEach((k, v) {
    if (v != null) payload[k] = v.toString();
  });

  List<WooCustomer> customers = [];
  _setApiResourceUrl(path: 'customers', queryParameters: payload);

  final response = await get(queryUri.toString());
  _printToLog('response gotten : ' + response.toString());
  for (var c in response) {
    var customer = WooCustomer.fromJson(c);
    _printToLog('customers here : ' + customer.toString());
    customers.add(customer);
  }
  return customers;
}