addTo method

Future<int?> addTo(
  1. RoomType roomType,
  2. dynamic entity, {
  3. int? playlistKey,
  4. bool ignoreDuplicate = true,
})

Used to add a specific song(entity) into a specific Room.

Parameters:

  • roomType this will define which room the entity will be added.
  • entity this is the entity with all information about song.
  • ignoreDuplicate this will ignore(or no) the song with the same id.
  • playlistKey this will be the id/key of the playlist *.

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

Important:

  • If the return is equal to 0, this song was already added.

Usage:

  • A entity is a Map<dynamic, dynamic>. You have two options:

    • 1 Create a manual entity.

    • 2 Use on_audio_query extension to create the entity.

Examples:

1- Manual

Map<dynamic, dynamic> entityMap = {
  "_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();

2- OnAudioQuery

List<SongModel> listOfSongs;
//toFavoritesEntity()
//toLastPlayedEntity()
//toMostPlayedEntity()
Map<dynamic, dynamic> entityMap = listOfSongs[index].getMap.toFavoritesEntity();

Return:

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

Throw:

  • Will throw a error when entity is null or when RoomType.PLAYLIST is called and playlistKey is null.

See also:

Implementation

Future<int?> addTo(
  RoomType roomType,
  dynamic entity, {
  int? playlistKey,
  bool ignoreDuplicate = true,
}) async {
  assert(
    OnEntityChecker(entity).isEntity(roomType),
    "[addTo] - [entity] 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.addToController(
    roomType,
    entity,
    playlistKey: playlistKey,
    ignoreDuplicate: ignoreDuplicate,
  );
}