submitDeleteEventually static method

Future<ParseResponse?> submitDeleteEventually({
  1. ParseClient? client,
  2. bool? autoSendSessionId,
})

Implementation

static Future<ParseResponse?> submitDeleteEventually(
    {ParseClient? client, bool? autoSendSessionId}) async {
  // preparation ParseCoreData
  final CoreStore coreStore = ParseCoreData().getStore();

  // delete
  // get json parse saved objects
  List<String>? listDeletes =
      await coreStore.getStringList(keyParseStoreDeletes);

  if (listDeletes != null) {
    int firstLength = listDeletes.length;
    List<String> elementsToRemove = [];
    for (var element in listDeletes) {
      // decode json
      dynamic object = json.decode(element);

      // crate parse object
      ParseObject parseObject = ParseObject(object[keyVarClassName],
              client: client, autoSendSessionId: autoSendSessionId)
          .fromJson(object);

      // delete parse object
      ParseResponse deleteResponse = await parseObject.delete();

      if (deleteResponse.success) {
        // remove from list Deletes
        elementsToRemove.add(element);
      }
    }

    // Remove the elements after the loop
    for (var elementToRemove in elementsToRemove) {
      listDeletes.remove(elementToRemove);
    }

    // set new list deletes in coreStore
    coreStore.setStringList(keyParseStoreDeletes, listDeletes);

    bool success = true;
    int statusCode = 200;

    if (listDeletes.length == firstLength) {
      // Nothing has been deleted
      success = false;
      statusCode = -1;
    }

    final ParseResponse response = ParseResponse()
      ..success = success
      ..results = <dynamic>[]
      ..statusCode = statusCode;

    return response;
  }

  return null;
}