updateData method

Future<DocumentSnapshot> updateData(
  1. Map<String, dynamic> data
)

Updates fields in the document referred to by this DocumentReference.

If no document exists yet, the update will fail.

Implementation

Future<DocumentSnapshot> updateData(Map<String, dynamic> data) async {
  // if we don't have the document ID, get it first.
  if (id == null) _id = (await get())?.id;

  // early out if this document does not exist
  if (_id == null) {
    throw CarpServiceException(message: 'No valid document id found.');
  }

  Map<String, dynamic> payload = {'name': name, 'data': data};

  final response = await service._put(
    documentUri,
    body: json.encode(payload),
  );

  int httpStatusCode = response.statusCode;
  Map<String, dynamic> responseJson =
      json.decode(response.body) as Map<String, dynamic>;

  if (httpStatusCode == HttpStatus.ok) {
    return DocumentSnapshot._(path, responseJson);
  } else {
    throw CarpServiceException.fromMap(httpStatusCode, responseJson);
  }
}