searchResultsProvider top-level property

AutoDisposeFutureProvider<SearchResult> searchResultsProvider
final

Provider for search results.

This provider performs a search whenever the query changes. It checks the cache first and falls back to the search provider if no cached result exists. Results are automatically cached for future use.

Implementation

final searchResultsProvider = FutureProvider.autoDispose<SearchResult>((
  ref,
) async {
  final query = ref.watch(searchQueryProvider);

  if (query.trim().isEmpty) {
    return SearchResult.empty(query);
  }

  // Initialize repositories
  final cacheRepo = ref.watch(searchCacheRepositoryProvider);
  await cacheRepo.initialize();

  final historyRepo = ref.watch(searchHistoryRepositoryProvider);
  await historyRepo.initialize();

  // Check cache first
  final cachedResult = cacheRepo.getCachedResult(query);
  if (cachedResult != null) {
    return cachedResult;
  }

  // Perform search
  final searchProvider = ref.watch(searchProviderProvider);
  final result = await searchProvider.search(query);

  // Cache the result
  await cacheRepo.cacheResult(result);

  // Add to history
  await historyRepo.addToHistory(query);

  return result;
});