get method Null safety

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

Get a document If the parameters are invalid null is returned.

Implementation

Future<dynamic> get(String id, [String rev = '']) {
  final opCompleter = Completer<dynamic>();
  if (id.isEmpty) {
    opCompleter.complete(null);
    return opCompleter.future;
  }
  /* Check for offline, if so try the get from local storage */
  if (!online) {
    _database.getLocalStorageObject(id).then((dynamic document) {
      final dynamic res = JsonObjectLite<dynamic>();
      res.localResponse = true;
      res.operation = getc;
      res.id = id;
      res.rev = null;
      if (document == null) {
        res.ok = false;
        res.payload = null;
      } else {
        res.ok = true;
        res.payload = document['payload'];
        res.rev = WiltUserUtils.getDocumentRev(res);
      }

      opCompleter.complete(res);
      if (_clientCompleter != null) {
        _completionResponse = _createCompletionResponse(res);
        _clientCompleter();
      }
    });
  } else {
    void completer(dynamic res) {
      /* If Ok update local storage with the document */
      res.operation = getc;
      res.id = id;
      res.localResponse = false;
      if (!res.error) {
        res.rev = WiltUserUtils.getDocumentRev(res.jsonCouchResponse);
        _database.updateLocalStorageObject(
            id, res.jsonCouchResponse, res.rev, _SporranDatabase.updatedc);
        res.ok = true;
        res.payload = res.jsonCouchResponse;
        /**
         * Get the documents attachments and create them locally
         */
        _database.createDocumentAttachments(id, res.payload);
      } else {
        res.ok = false;
        res.payload = null;
        res.rev = null;
      }

      opCompleter.complete(res);
      if (_clientCompleter != null) {
        _completionResponse = _createCompletionResponse(res);
        _clientCompleter();
      }
    }

    /* Get the document from CouchDb with its attachments */
    final wiltRev = rev.isNotEmpty ? rev : null;
    _database.wilt.getDocument(id, wiltRev, true).then(completer);
  }

  return opCompleter.future;
}