getTopTracks method

Future<List<Track>> getTopTracks({
  1. required String country,
  2. String? location,
  3. int page = 1,
  4. int limit = 50,
})

Get the most popular tracks on Last.fm last week by country.

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

Implementation

Future<List<Track>> getTopTracks({
  required String country,
  String? location,
  int page = 1,
  int limit = 50,
}) async {
  final parameters = {
    'country': country,
    'location': location,
    'page': page,
    'limit': limit,
  };

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

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

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

  if (topTracks == null) {
    return [];
  } else {
    // This operation is necessary because the tracks have different duration.
    var fixTopTracks = List.generate(
      (topTracks as List).length,
      (i) => Track.fromJson(topTracks[i]),
    );

    for (var track in fixTopTracks) {
      track.duration = track.duration != null ? track.duration! * 1000 : null;
    }
    return fixTopTracks;
  }
}