bulkCreate method Null safety

Future bulkCreate(
  1. Map<String, JsonObjectLite> docList
)

Bulk document create.

docList is a map of documents with their keys. If the parameters are invalid null is returned.

Implementation

Future<dynamic> bulkCreate(Map<String, JsonObjectLite<dynamic>> docList) {
  final opCompleter = Completer<dynamic>();
  if (docList.isEmpty) {
    opCompleter.complete(null);
    return opCompleter.future;
  }
  /* Futures list for LawnDart update */
  final updateList = <Future<dynamic>>[];

  /* Update LawnDart */
  docList.forEach((dynamic key, dynamic document) {
    updateList.add(_database.updateLocalStorageObject(
        key, document, '', _SporranDatabase.notUpdatedc));
  });

  /* Wait for Lawndart */
  Future.wait(updateList)
      // ignore: missing_return
      .then((_) {
    /* If we are offline just return */
    if (!online) {
      final dynamic res = JsonObjectLite<dynamic>();
      res.localResponse = true;
      res.operation = bulkCreatec;
      res.ok = true;
      res.payload = docList;
      res.id = null;
      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.operation = bulkCreatec;
      res.id = null;
      res.payload = docList;
      res.rev = null;
      if (!res.error) {
        /* Get the revisions for the updates */
        final JsonObjectLite<dynamic> couchResp = res.jsonCouchResponse;
        final revisions = <JsonObjectLite<dynamic>?>[];
        final revisionsMap = <String, String>{};

        for (final dynamic resp in couchResp.toList()) {
          try {
            revisions.add(resp);
            revisionsMap[resp.id] = resp.rev;
          } on Exception {
            revisions.add(null);
          }
        }
        res.rev = revisions;

        /* Update the documents */
        docList.forEach((String key, dynamic document) {
          _database.updateLocalStorageObject(
              key, document, revisionsMap[key]!, _SporranDatabase.updatedc);
        });

        res.ok = true;
      }

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

    /* Prepare the documents */
    final documentList = <String>[];
    docList.forEach((dynamic key, dynamic document) {
      final docString = WiltUserUtils.addDocumentId(document, key);
      documentList.add(docString);
    });

    final docs = WiltUserUtils.createBulkInsertString(documentList);

    /* Do the bulk create*/
    _database.wilt.bulkString(docs).then(completer);
  });

  return opCompleter.future;
}