getOne method

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

Returns single item by its id.

Throws 404 ClientException in case an empty id is provided.

Implementation

Future<M> getOne(
  String id, {
  String? expand,
  String? fields,
  Map<String, dynamic> query = const {},
  Map<String, String> headers = const {},
}) async {
  if (id.isEmpty) {
    throw ClientException(
      url: client.buildUrl("$baseCrudPath/"),
      statusCode: 404,
      response: <String, dynamic>{
        "code": 404,
        "message": "Missing required record id.",
        "data": <String, dynamic>{},
      },
    );
  }

  final enrichedQuery = Map<String, dynamic>.of(query);
  enrichedQuery["expand"] ??= expand;
  enrichedQuery["fields"] ??= fields;

  return client
      .send(
        "$baseCrudPath/${Uri.encodeComponent(id)}",
        query: enrichedQuery,
        headers: headers,
      )
      .then((data) => itemFactoryFunc(data as Map<String, dynamic>? ?? {}));
}