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 this.get())?.id;
  if (_id == null) // early out if this document does not exist
    throw CarpServiceException(message: 'No valid document id found.');

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

  http.Response response = await httpr.put(Uri.encodeFull(documentUri),
      headers: headers, body: json.encode(payload));

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

  if (httpStatusCode == HttpStatus.ok)
    return DocumentSnapshot._(path, responseJson);

  throw CarpServiceException(
    httpStatus: HTTPStatus(httpStatusCode, response.reasonPhrase),
    message: responseJson["message"],
    path: responseJson["path"],
  );
}