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;
  }

  if (objectId == null) {
    return ParseResponse()..success = false;
  }

  try {
    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) {
      return ParseResponse()..success = true;
    } else {
      _objectData[key] = object;
      _unsavedChanges[key] = object;
      _savingChanges[key] = object;

      return response;
    }
  } on Exception catch (e) {
    _objectData[key] = object;
    _unsavedChanges[key] = object;
    _savingChanges[key] = object;

    return handleException(e, ParseApiRQ.unset, _debug, parseClassName);
  }
}