unset method

Future<ParseResponse> unset(
  1. String key,
  2. {bool offlineOnly = false}
)

Can be used set an objects variable to undefined rather than null

If object is not saved remotely, set offlineOnly to true to avoid api calls.

Implementation

Future<ParseResponse> unset(String key, {bool offlineOnly = false}) async {
  final dynamic object = _objectData[key];
  _objectData.remove(key);
  _unsavedChanges.remove(key);
  _savingChanges.remove(key);

  if (offlineOnly) {
    return ParseResponse()..success = true;
  }

  try {
    if (objectId != null) {
      final Uri url = getSanitisedUri(_client, '$_path/$objectId');
      final String body = '{\"$key\":{\"__op\":\"Delete\"}}';
      final ParseNetworkResponse result =
          await _client.put(url.toString(), data: body);
      final ParseResponse response = handleResponse<ParseObject>(
          this, result, ParseApiRQ.unset, _debug, parseClassName);
      if (!response.success) {
        _objectData[key] = object;
        _unsavedChanges[key] = object;
        _savingChanges[key] = object;
      } else {
        return ParseResponse()..success = true;
      }
    }
  } on Exception {
    _objectData[key] = object;
    _unsavedChanges[key] = object;
    _savingChanges[key] = object;
  }

  return ParseResponse()..success = false;
}