onSubmitted method

void onSubmitted(
  1. String query
)

Called when the user has submitted the search query.

Implementation

void onSubmitted(String query) async {
  query = query.trim();

  if (!_isSearching) {
    pagingController = null;

    setState(() {
      _isSearching = true;
    });

    pagingController = PagingController(firstPageKey: 1);
    try {
      List<MediaItem>? newItems = await mediaSource.searchMediaItems(
        context: context,
        searchTerm: query,
        pageKey: 1,
      );
      if (newItems != null && newItems.isNotEmpty) {
        pagingController?.appendPage(newItems, 2);
      }
    } catch (e) {
      pagingController?.appendLastPage([]);
    }
    pagingController?.addPageRequestListener((pageKey) async {
      try {
        List<MediaItem>? newItems = await mediaSource.searchMediaItems(
          context: context,
          searchTerm: query,
          pageKey: pageKey,
        );
        if (newItems != null && newItems.isNotEmpty) {
          pagingController?.appendPage(newItems, pageKey);
        }
      } catch (e) {
        pagingController?.appendLastPage([]);
      }
    });
    appModel.addToSearchHistory(
      historyKey: mediaSource.uniqueKey,
      searchTerm: mediaType.floatingSearchBarController.query,
    );

    setState(() {
      _isSearching = false;
    });
  }
}