addToCollection<T> static method

Future addToCollection<T>(
  1. String key, {
  2. required dynamic item,
  3. bool allowDuplicates = true,
  4. Map<Type, dynamic>? modelDecoders,
})

Add a newItem to the collection using a key.

Implementation

static Future addToCollection<T>(String key,
    {required dynamic item,
    bool allowDuplicates = true,
    Map<Type, dynamic>? modelDecoders}) async {
  List<T> collection =
      await readCollection<T>(key, modelDecoders: modelDecoders);
  if (allowDuplicates == false) {
    if (collection.any((collect) => collect == item)) {
      return;
    }
  }
  collection.add(item);
  await saveCollection<T>(key, collection);
}