searchForTrack method

  1. @override
Stream<LazySpotifyResult> searchForTrack(
  1. String query, [
  2. int itemCount = 5
])
override

Searches for tracks on Spotify.

Implementation

// NOTE: This should be in the [TrackSearch] extension, but is implemented here to keep in line
// with the [SrcEngine] interface.
@override
Stream<LazySpotifyResult> searchForTrack(String query, [int itemCount = 5]) async* {
  var resultPages =
      await _spotifyEngine.search.get(query, types: [SearchType.track]).first(itemCount);

  var results = <LazySpotifyResult>[];

  for (var page in resultPages) {
    if (page.items != null) {
      // `page.items` is apparently a `MappedListIterable`, whatever that is,
      // so I'd rathe typecast the `item` instead.
      for (var item in page.items!) {
        var track = item as Track;

        yield LazySpotifyResult(
          artists: () async* {
            if (track.artists != null) {
              for (var artist in track.artists!) {
                yield artist.name!;
              }
            }
          },
          title: () async => track.name ?? '',
          album: () async => track.album?.name ?? '',
          sDuration: () async => track.durationMs! ~/ 1000,
          srcUrl: () async => 'https://open.spotify.com/track/${track.uri?.split(':').last}',
          dlUrl: () async => '', // Spotify does not provide a direct download URL.
          artUrl: () async => track.album?.images?.first.url ?? '',
          diskNumber: track.discNumber ?? 0,
          trackNumber: track.trackNumber ?? 0,
        );
      }
    }
  }
}