putAttachment method Null safety

Future putAttachment(
  1. String id,
  2. dynamic attachment
)

Put attachment

If the revision is supplied the attachment to the document will be updated, otherwise the attachment will be created, along with the document if needed.

The JsonObjectLite attachment parameter must contain the following :-

String attachmentName String rev - maybe '', see above String contentType - mime type in the form 'image/png' String payload - stringified binary blob. If the parameters are invalid null is returned.

Implementation

Future<dynamic> putAttachment(String id, dynamic attachment) {
  final opCompleter = Completer<dynamic>();
  if (id.isEmpty) {
    opCompleter.complete(null);
    return opCompleter.future;
  }
  if (attachment == null) {
    opCompleter.complete(null);
    return opCompleter.future;
  }
  /* Update LawnDart */
  final key = '$id-${attachment.attachmentName}-'
      '${_SporranDatabase.attachmentMarkerc}';
  _database
      .updateLocalStorageObject(
          key, attachment, attachment.rev, _SporranDatabase.notUpdatedc)
      // ignore: missing_return
      .then((_) {
    /* If we are offline just return */
    if (!online) {
      final dynamic res = JsonObjectLite<dynamic>();
      res.localResponse = true;
      res.operation = putAttachmentc;
      res.ok = true;
      res.payload = attachment;
      res.id = id;
      res.rev = null;
      opCompleter.complete(res);
      if (_clientCompleter != null) {
        _completionResponse = _createCompletionResponse(res);
        _clientCompleter();
      }
      return opCompleter.future;
    }

    /* Complete locally, then boomerang to the client */
    void completer(dynamic res) {
      /* If success, mark the update as UPDATED in local storage */
      res.ok = false;
      res.localResponse = false;
      res.id = id;
      res.operation = putAttachmentc;
      res.rev = null;
      res.payload = null;

      if (!res.error) {
        final dynamic newAttachment =
            JsonObjectLite<dynamic>.fromJsonString(_mapToJson(attachment));
        newAttachment.contentType = attachment.contentType;
        newAttachment.payload = attachment.payload;
        newAttachment.attachmentName = attachment.attachmentName;
        res.payload = newAttachment;
        res.rev = res.jsonCouchResponse.rev;
        newAttachment.rev = res.jsonCouchResponse.rev;
        _database.updateLocalStorageObject(key, newAttachment,
            res.jsonCouchResponse.rev, _SporranDatabase.updatedc);
        _database.updateAttachmentRevisions(id, res.jsonCouchResponse.rev);
        res.ok = true;
      }
      opCompleter.complete(res);
      if (_clientCompleter != null) {
        _completionResponse = _createCompletionResponse(res);
        _clientCompleter();
      }
    }

    /* Do the create */
    if (attachment.rev == '') {
      _database.wilt
          .createAttachment(id, attachment.attachmentName, attachment.rev,
              attachment.contentType, attachment.payload)
          .then(completer);
    } else {
      _database.wilt
          .updateAttachment(id, attachment.attachmentName, attachment.rev,
              attachment.contentType, attachment.payload)
          .then(completer);
    }
  });

  return opCompleter.future;
}