put method Null safety

Future put(
  1. String id,
  2. JsonObjectLite document,
  3. [String rev = '']
)

Update document.

If the document does not exist a create is performed.

For an update operation a specific revision must be specified. If the parameters are invalid null is returned.

Implementation

Future<dynamic> put(String id, JsonObjectLite<dynamic> document,
    [String rev = '']) {
  final opCompleter = Completer<dynamic>();
  if (id.isEmpty) {
    opCompleter.complete(null);
    return opCompleter.future;
  }
  /* Update LawnDart */
  _database
      .updateLocalStorageObject(
          id, document, rev, _SporranDatabase.notUpdatedc)
      // ignore: missing_return
      .then((_) {
    /* If we are offline just return */
    if (!online) {
      final dynamic res = JsonObjectLite<dynamic>();
      res.localResponse = true;
      res.operation = putc;
      res.ok = true;
      res.payload = document;
      res.id = id;
      res.rev = rev;

      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 = putc;
      res.id = id;
      res.payload = document;
      if (!res.error) {
        res.rev = res.jsonCouchResponse.rev;
        _database.updateLocalStorageObject(
            id, document, rev, _SporranDatabase.updatedc);
        _database.updateAttachmentRevisions(id, rev);

        res.ok = true;
      } else {
        res.rev = null;
      }

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

    /* Do the put */
    final wiltRev = rev.isNotEmpty ? rev : null;
    _database.wilt.putDocument(id, document, wiltRev).then(completer);
  });

  return opCompleter.future;
}