getTopAlbums method

Future<List<SimplifiedAlbum>> getTopAlbums(
  1. String artistId
)

Retrieves a list of an artist's top albums in a specific market.

  • artistId: The Spotify ID of the artist.

Returns a list of SimplifiedAlbum objects representing the artist's top albums.

Implementation

Future<List<SimplifiedAlbum>> getTopAlbums(String artistId) async {
  try {
    // Make an API request to get artist's albums
    final response = await _dio.get(
      'https://api.spotify.com/v1/artists/$artistId/albums',
      queryParameters: {
        'include_groups': 'album',
        'market': Market.AM.name, // Replace with the desired market
      },
    );

    // Parse the response into a list of SimplifiedAlbum objects
    final albums = (response.data['items'] as List)
        .map((album) => SimplifiedAlbum.fromMap(album))
        .toList();
    return albums;
  } catch (e) {
    throw Exception('Error fetching top albums: $e');
  }
}