getTopArtists method

Future<List<Artist>?> getTopArtists({
  1. required String user,
  2. Period? period,
  3. int page = 1,
  4. int limit = 50,
})

Get the top artists listened to by a user. You can stipulate a time period. Sends the overall chart by default.

NOTE: the output list is already ordered by rank.

https://www.last.fm/api/show/user.getTopArtists

Implementation

Future<List<Artist>?> getTopArtists({
  required String user,
  Period? period,
  int page = 1,
  int limit = 50,
}) async {
  final parameters = {
    'user': user,
    'period': period?.value,
    'page': page,
    'limit': limit,
  };

  final request = Request(
    api: _api,
    method: 'user.getTopArtists',
    parameters: parameters,
  );

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

  final topArtist = response['topartists']['artist'];

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