createPlaylist method
Creates a new empty playlist and returns its id.
titlePlaylist title.descriptionPlaylist description.privacyStatusPlaylists can bePUBLIC,PRIVATE, orUNLISTED. (Default:PRIVATE).videoIdsIDs of songs to create the playlist with.sourcePlaylistAnother 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;
}