delete method Null safety

Future delete(
  1. String id,
  2. [String rev = '']
)

Delete a document.

Revision must be supplied if we are online. If the parameters are invalid null is returned.

Implementation

Future<dynamic> delete(String id, [String rev = '']) {
  final opCompleter = Completer<dynamic>();
  if (id.isEmpty) {
    opCompleter.complete(null);
    return opCompleter.future;
  }
  /* Remove from Lawndart */
  _database.lawndart.getByKey(id).then((dynamic document) {
    if (document != null) {
      _database.lawndart.removeByKey(id)
          // ignore: missing_return
          .then((_) {
        /* Check for offline, if so add to the pending delete queue
            and return */
        if (!online) {
          _database.addPendingDelete(id, document);
          final dynamic res = JsonObjectLite<dynamic>();
          res.localResponse = true;
          res.operation = deletec;
          res.ok = true;
          res.id = id;
          res.payload = null;
          res.rev = rev;
          opCompleter.complete(res);
          if (_clientCompleter != null) {
            _completionResponse = _createCompletionResponse(res);
            _clientCompleter();
          }
          return opCompleter.future;
        } else {
          /* Online, delete from CouchDb */
          void completer(dynamic res) {
            res.operation = deletec;
            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(id);
            opCompleter.complete(res);
            if (_clientCompleter != null) {
              _completionResponse = _createCompletionResponse(res);
              _clientCompleter();
            }
          }

          /* Delete the document from CouchDB */
          final wiltRev = rev.isNotEmpty ? rev : null;
          _database.wilt.deleteDocument(id, wiltRev).then(completer);
        }
      });
    } else {
      /* Doesnt exist, return error */
      final dynamic res = JsonObjectLite<dynamic>();
      res.localResponse = true;
      res.operation = deletec;
      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;
}