createPlaylist method

Future createPlaylist(
  1. String title,
  2. String description, {
  3. String privacyStatus = 'PRIVATE',
  4. List<String>? videoIds,
  5. String? sourcePlaylist,
})

Creates a new empty playlist and returns its id.

  • title Playlist title.
  • description Playlist description.
  • privacyStatus Playlists can be PUBLIC, PRIVATE, or UNLISTED. (Default: PRIVATE).
  • videoIds IDs of songs to create the playlist with.
  • sourcePlaylist Another playlist whose songs should be added to the new playlist.

Returns ID of the YouTube playlist or full response if there was an error.

Implementation

Future<dynamic> createPlaylist(
  String title,
  String description, {
  String privacyStatus = 'PRIVATE',
  List<String>? videoIds,
  String? sourcePlaylist,
}) async {
  checkAuth();

  final invalidCharacters = [
    '<',
    '>',
  ]; // ytmusic will crash if these are part of the title
  final invalidFound =
      invalidCharacters.where((c) => title.contains(c)).toList();
  if (invalidFound.isNotEmpty) {
    throw YTMusicUserError(
      '$title contains invalid characters: ${invalidFound.join(', ')}',
    );
  }

  final body = <String, dynamic>{
    'title': title,
    'description': htmlToTxt(description), // YT does not allow HTML tags
    'privacyStatus': privacyStatus,
  };

  if (videoIds != null) body['videoIds'] = videoIds;
  if (sourcePlaylist != null) body['sourcePlaylistId'] = sourcePlaylist;

  const endpoint = 'playlist/create';
  final response = await sendRequest(endpoint, body);
  return response.containsKey('playlistId')
      ? response['playlistId']
      : response;
}