getTopTracks method

Future<List<TrackModel>> getTopTracks(
  1. String artistId
)

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

  • artistId: The Spotify ID of the artist.

Returns a list of TrackModel objects representing the artist's top tracks.

Implementation

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

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