searchMediaItems method

  1. @override
Future<List<MediaItem>?> searchMediaItems(
  1. {required BuildContext context,
  2. required String searchTerm,
  3. required int pageKey}
)
override

This returns a list of MediaItem, and is performed to search the media source for items.

Implementation

@override
Future<List<MediaItem>?> searchMediaItems({
  required BuildContext context,
  required String searchTerm,
  required int pageKey,
}) async {
  late VideoSearchList? searchList;

  searchTerm = searchTerm.trim();

  String storeKey = searchTerm;
  if (isCaptionFilterOn) {
    storeKey = '$searchTerm [filter:cc]';
  }

  _searchListCache[storeKey] ??= {};
  Map<int, VideoSearchList> store = _searchListCache[storeKey]!;

  if (pageKey == 1) {
    if (store[1] == null) {
      searchList = await _searchClient.search(searchTerm,
          filter: isCaptionFilterOn
              ? FeatureFilters.subTitles
              : TypeFilters.video);
    } else {
      searchList = store[1];
    }
  } else {
    VideoSearchList lastList = store[pageKey - 1]!;
    searchList = await lastList.nextPage();
  }

  if (searchList == null) {
    return null;
  } else {
    store[pageKey] = searchList;
  }

  List<MediaItem> items = [];
  List<Video> videos = [];

  for (Video video in searchList) {
    if (video.duration == null ||
        video.duration == Duration.zero ||
        video.isLive) {
      continue;
    }

    videos.add(video);
    _videoCache[video.url] = video;
    items.add(getMediaItem(video));
  }

  return items;
}