getAllDocs method Null safety

Future<JsonObjectLite> getAllDocs(
  1. {bool includeDocs = false,
  2. int limit = 10,
  3. String startKey = '',
  4. String endKey = '',
  5. List<String> keys = const <String>[],
  6. bool descending = false}
)

Get all documents.

The parameters should be self explanatory and are addative.

In offline mode only the keys parameter is respected. The includeDocs parameter is also forced to true.

Implementation

Future<JsonObjectLite<dynamic>> getAllDocs(
    {bool includeDocs = false,
    int limit = 10,
    String startKey = '',
    String endKey = '',
    List<String> keys = const <String>[],
    bool descending = false}) {
  final opCompleter = Completer<JsonObjectLite<dynamic>>();

  /* Check for offline, if so try the get from local storage */
  if (!online) {
    if (keys.isEmpty) {
      /* Get all the keys from Lawndart */
      _database.lawndart.keys().toList().then((dynamic keyList) {
        /* Only return documents */
        final docList = <String>[];
        keyList.forEach((dynamic key) {
          final List<String> temp = key.split('-');
          if ((temp.length == 3) &&
              (temp[2] == _SporranDatabase.attachmentMarkerc)) {
            /* Attachment, discard the key */

          } else {
            docList.add(key);
          }
        });

        _database.getLocalStorageObjects(docList).then((dynamic documents) {
          final dynamic res = JsonObjectLite<dynamic>();
          res.localResponse = true;
          res.operation = getAllDocsc;
          res.id = null;
          res.rev = null;
          if (documents == null) {
            res.ok = false;
            res.payload = null;
          } else {
            res.ok = true;
            res.payload = documents;
            res.totalRows = documents.length;
            res.keyList = documents.keys.toList();
          }

          opCompleter.complete(res);
          if (_clientCompleter != null) {
            _completionResponse = _createCompletionResponse(res);
            _clientCompleter();
          }
        });
      });
    } else {
      _database.getLocalStorageObjects(keys).then((dynamic documents) {
        final dynamic res = JsonObjectLite<dynamic>();
        res.localResponse = true;
        res.operation = getAllDocsc;
        res.id = null;
        res.rev = null;
        if (documents == null) {
          res.ok = false;
          res.payload = null;
        } else {
          res.ok = true;
          res.payload = documents;
          res.totalRows = documents.length;
          res.keyList = documents.keys.toList();
        }

        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 = getAllDocsc;
      res.id = null;
      res.rev = null;
      res.localResponse = false;
      if (!res.error) {
        res.ok = true;
        res.payload = res.jsonCouchResponse;
      } else {
        res.localResponse = false;
        res.ok = false;
        res.payload = null;
      }

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

    /* Get the document from CouchDb */
    _database.wilt
        .getAllDocs(
            includeDocs: includeDocs,
            limit: limit,
            startKey: startKey,
            endKey: endKey,
            keys: keys,
            descending: descending)
        .then(completer);
  }

  return opCompleter.future;
}