getArtistSongs method

Future<List<SongDetailed>> getArtistSongs(
  1. String artistId
)

Retrieves a list of songs by a specific artist given the artist's ID.

Implementation

Future<List<SongDetailed>> getArtistSongs(String artistId) async {
  final artistData =
      await constructRequest("browse", body: {"browseId": artistId});
  final browseToken =
      traverse(artistData, ["musicShelfRenderer", "title", "browseId"]);

  if (browseToken is List) {
    return [];
  }

  final songsData =
      await constructRequest("browse", body: {"browseId": browseToken});
  final continueToken = traverse(songsData, ["continuation"]);
  late final Map moreSongsData;

  if (continueToken is String) {
    moreSongsData = await constructRequest(
      "browse",
      query: {"continuation": continueToken},
    );
  } else {
    moreSongsData = {};
  }

  return [
    ...traverseList(songsData, ["musicResponsiveListItemRenderer"]),
    ...traverseList(moreSongsData, ["musicResponsiveListItemRenderer"]),
  ]
      .map((s) => SongParser.parseArtistSong(
            s,
            ArtistBasic(
              artistId: artistId,
              name: traverseString(artistData, ["header", "title", "text"]) ??
                  '',
            ),
          ))
      .toList();
}