saveToCollection method

Future saveToCollection({
  1. bool inBackpack = false,
})

Save an item to a collection.

Example:

User userAnthony = User(name: 'Anthony');
await userAnthony.saveToCollection();

User userKyle = User(name: 'Kyle');
await userKyle.saveToCollection();

// Get the collection back with the users included.
List<User> users = await NyStorage.read<List<User>>('user');

You can also save the collection to the Backpack class using inBackpack.

Implementation

Future saveToCollection({bool inBackpack = false}) async {
  if (_key == null) {
    NyLogger.error(
      'static StorageKey key = "${(runtimeType.toString()).snakeCase}" is not defined for ${runtimeType.toString()}, please define a key.'
      '\n\nExample:\nstatic StorageKey key = "${(runtimeType.toString()).snakeCase}";\n${runtimeType.toString()}() : super(key: key);'
      '',
    );
    return;
  }
  await NyStorage.addToCollection<T>(_key!, item: this as T);
  if (inBackpack) {
    Backpack.instance.save(_key!, this);
  }
}