deleteItem method

Future<bool> deleteItem(
  1. dynamic itemId, {
  2. bool log = true,
})

Implementation

Future<bool> deleteItem(itemId, {bool log = true}) async {
  var collectionData = await get();

  if (_isMap(collectionData)) {
    if ((collectionData as Map).containsKey(itemId)) {
      collectionData.remove(itemId);
    } else {
      throw const StorageDatabaseException('Undefined item id');
    }
  } else if (_isList(collectionData)) {
    if (itemId >= 0 && (collectionData as List).length >= itemId) {
      collectionData.removeAt(itemId);
    } else {
      throw const StorageDatabaseException('Undefined item id');
    }
  } else {
    throw const StorageDatabaseException(
      'This Collection doesn\'t support collections',
    );
  }

  await set(collectionData, keepData: false);
  if (log) {
    for (String streamId in storageListeners.getPathStreamIds(path)) {
      if (storageListeners.hasStreamId(path, streamId)) {
        storageListeners.setDate(path, streamId);
      }
    }
  }
  return true;
}