getFirstListItem method

Future<M> getFirstListItem(
  1. String filter, {
  2. String? expand,
  3. String? fields,
  4. Map<String, dynamic> query = const {},
  5. Map<String, String> headers = const {},
})

Returns the first found list item by the specified filter.

Internally it calls getList() and returns its first item.

For consistency with getOne, this method will throw a 404 ClientException if no item was found.

Implementation

Future<M> getFirstListItem(
  String filter, {
  String? expand,
  String? fields,
  Map<String, dynamic> query = const {},
  Map<String, String> headers = const {},
}) {
  return getList(
    perPage: 1,
    skipTotal: true,
    filter: filter,
    expand: expand,
    fields: fields,
    query: query,
    headers: headers,
  ).then((result) {
    if (result.items.isEmpty) {
      throw ClientException(
        statusCode: 404,
        response: <String, dynamic>{
          "code": 404,
          "message": "The requested resource wasn't found.",
          "data": <String, dynamic>{},
        },
      );
    }

    return result.items.first;
  });
}