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};

  debug('REQUEST: PUT $documentUri\n$payload');

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

  int httpStatusCode = response.statusCode;
  debug('RESPONSE: $httpStatusCode\n${response.body}');
  Map<String, dynamic> responseJson =
      json.decode(response.body) as Map<String, dynamic>;

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

  print('$httpStatusCode - ${response.reasonPhrase}');
  print(responseJson["message"]);
  print(responseJson["path"]);

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