updateCollectionByIndex<T> static method

Future<bool> updateCollectionByIndex<T>(
  1. int index,
  2. T object(
    1. T item
    ), {
  3. required String key,
})

Update a value in the local storage by index.

Implementation

static Future<bool> updateCollectionByIndex<T>(
    int index, T Function(T item) object,
    {required String key}) async {
  List<T> collection = await readCollection<T>(key);

  // Check if the collection is empty or the index is out of bounds
  if (collection.isEmpty || index < 0 || index >= collection.length) {
    NyLogger.error(
        '[NyStorage.updateCollectionByIndex] The collection is empty or the index is out of bounds.');
    return false;
  }

  // Update the item
  T newItem = object(collection[index]);

  collection[index] = newItem;

  await saveCollection<T>(key, collection);
  return true;
}