deleteItem method

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

Implementation

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

  if (collectionData is Map) {
    if (collectionData.containsKey(itemId)) {
      collectionData.remove(itemId);
    } else {
      throw const StorageDatabaseException('Undefined item id');
    }
  } else if (collectionData is List) {
    if (itemId >= 0 && collectionData.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 (stream) {
    for (String streamId in storageListeners.getPathStreamIds(path)) {
      if (storageListeners.hasStreamId(path, streamId)) {
        storageListeners.setDate(path, streamId);
      }
    }
  }
  return true;
}