deleteAttachment method Null safety

Future deleteAttachment(
  1. String id,
  2. String attachmentName,
  3. [String rev = '']
)

Delete an attachment. Revision can be null if offline. If the parameters are invalid null is returned.

Implementation

Future<dynamic> deleteAttachment(String id, String attachmentName,
    [String rev = '']) {
  final opCompleter = Completer<dynamic>();
  if (id.isEmpty) {
    opCompleter.complete(null);
    return opCompleter.future;
  }
  if (attachmentName.isEmpty) {
    opCompleter.complete(null);
    return opCompleter.future;
  }
  final key = '$id-$attachmentName-${_SporranDatabase.attachmentMarkerc}';

  /* Remove from Lawndart */
  _database.lawndart.getByKey(key).then((dynamic document) {
    if (document != null) {
      _database.lawndart.removeByKey(key)
          // ignore: missing_return
          .then((_) {
        /* Check for offline, if so add to the pending delete
            queue and return */
        if (!online) {
          _database.addPendingDelete(key, document);
          final dynamic res = JsonObjectLite<dynamic>();
          res.localResponse = true;
          res.operation = deleteAttachmentc;
          res.ok = true;
          res.id = id;
          res.payload = null;
          res.rev = null;
          opCompleter.complete(res);
          if (_clientCompleter != null) {
            _completionResponse = _createCompletionResponse(res);
            _clientCompleter();
          }
          return opCompleter.future;
        } else {
          /* Online, delete from CouchDb */
          void completer(dynamic res) {
            res.operation = deleteAttachmentc;
            res.localResponse = false;
            res.payload = res.jsonCouchResponse;
            res.id = id;
            res.rev = null;
            if (res.error) {
              res.ok = false;
            } else {
              res.ok = true;
              res.rev = res.jsonCouchResponse.rev;
            }
            _database.removePendingDelete(key);
            opCompleter.complete(res);
            if (_clientCompleter != null) {
              _completionResponse = _createCompletionResponse(res);
              _clientCompleter();
            }
          }

          /* Delete the attachment from CouchDB */
          final wiltRev = rev.isNotEmpty ? rev : null;
          _database.wilt
              .deleteAttachment(id, attachmentName, wiltRev)
              .then(completer);
        }
      });
    } else {
      /* Doesn't exist, return error */
      final dynamic res = JsonObjectLite<dynamic>();
      res.localResponse = true;
      res.operation = deleteAttachmentc;
      res.id = id;
      res.payload = null;
      res.rev = null;
      res.ok = false;
      opCompleter.complete(res);
      if (_clientCompleter != null) {
        _completionResponse = _createCompletionResponse(res);
        _clientCompleter();
      }
    }
  });

  return opCompleter.future;
}