getTopTracks method

Future<List<Track>?> getTopTracks({
  1. String? artist,
  2. String? mbid,
  3. int page = 1,
  4. int limit = 50,
  5. bool autoCorrect = false,
})

Get the top tracks by an artist on Last.fm, ordered by popularity.

https://www.last.fm/api/show/artist.getTopTracks

Implementation

Future<List<Track>?> getTopTracks({
  String? artist,
  String? mbid,
  int page = 1,
  int limit = 50,
  bool autoCorrect = false,
}) async {
  if (artist == null && mbid == null) {
    return Future.error(
      ScrobblenautException(
        description: 'This method requires at least artist or mbid.',
      ),
    );
  }

  final parameters = {
    'artist': artist,
    'mbid': mbid,
    'page': page,
    'limit': limit,
    'autocorrect': (autoCorrect ? 1 : 0),
  };

  final request = Request(
    api: _api,
    method: 'artist.getTopTracks',
    parameters: parameters,
  );

  final response = await request.send(mode: RequestMode.GET);

  final topTracks = response['toptracks']['track'];

  return topTracks == null
      ? null
      : List.generate(
          (topTracks as List).length,
          (i) => Track.fromJson(topTracks[i]),
        );
}