addAllTo method

Future<List<int>> addAllTo(
  1. RoomType roomType,
  2. List entities, {
  3. int? playlistKey,
})

Used to add a list of song(entity) to a specific room.

Parameters:

  • roomType this will define which room the entities will be added.
  • entities this is the entities with all information about songs.
  • playlistKey this will be the id/key of the playlist *.

* playlistKey it's only required when using RoomType.PLAYLIST.

Important:

  • For now if you add two entities with the same information, will be added. You can use checkIn to make sure that the song was already added.

Usage:

  • All entities are a List<Map<dynamic, dynamic>>. You have two options:

    • 1 Create a manual entities.

    • 2 Use on_audio_query extension to create the entities.

Examples:

1- Manual

//Create a [Map<dynamic, dynamic>] and convert to a [Entity]
Map<dynamic, dynamic> entityMap = {
  "last_data": song.data,
  "display_name": song.displayName,
  "id": song.id,
  "album": song.album,
  "album_id": song.albumId,
  "artist": song.artist,
  "artist_id": song.artistId,
  "date_added": song.dateAdded,
  "duration": song.duration,
  "title": song.title,
  "artwork": song.artwork,
};

// After this, you can call this method to convert to [FavoritesEntity]:
var favoritesEntity = entityMap.toFavoritesEntity();

//Now, add to a list of entity
List<FavoritesEntity> entities = [];
entities.add(favoritesEntity);

2- OnAudioQuery

List<SongModel> listOfSongs;
//toFavoritesEntity()
//toLastPlayedEntity()
//toMostPlayedEntity()
List<FavoritesEntity> entities = [];

listOfSongs.forEach((element)
  entities.add(element.getMap.toFavoritesEntity());
});

Return:

  • If the list of song(entity) was added, will return a list of int that represent the songs ids inside the room, if some problem occurred, will throw a error.

Throw:

  • Will throw a error when some of the entity is null or when RoomType.PLAYLIST_SONG is called and playlistId is null.

See too:

Implementation

Future<List<int>> addAllTo(
  RoomType roomType,
  List<dynamic> entities, {
  int? playlistKey,
}) async {
  assert(
      OnEntityChecker(entities).isEntity(roomType),
      "[addAllTo] - [entities] isn't a instance of Entity\n"
      "Choose one of the existing entities");
  if (roomType == RoomType.PLAYLIST && playlistKey == null) {
    throw Exception(
      "Cannot add [entity] to a undefined [playlist]\n"
      "Define the [playlistKey] parameter.",
    );
  }
  return await _controller.addAllToController(
    roomType,
    entities,
    playlistKey: playlistKey,
  );
}