Module 2
Topic 7

Search

Implementing efficient search functionality — from local filtering to real-time API search with debouncing.

Flutter Cookbook
What Is Search?

Search allows users to find specific items within a dataset by typing a query. It's essential for apps with large amounts of data, such as:

  • E-commerce apps (search for products)
  • Social media apps (search for users or posts)
  • Messaging apps (search for messages or contacts)
  • Content apps (search for articles or videos)

🎯 Two Approaches

  • Local Search – Search within data already loaded in the app
  • API Search – Send the search query to a server and get results
Search Strategies

There are three main search strategies used in Flutter apps:

🔍
Local Filtering
Filter already-loaded data in memory
🌐
API Search
Send query to server for results
Real-Time Search
Search as you type (with debouncing)
Local Search (Filtering)

Local search filters data that's already loaded in memory. It's fast and works offline.

✅ When to Use Local Search

  • Small to medium datasets (under 10,000 items)
  • Data is already loaded in memory
  • Need offline search capability
  • Simple filtering by text
API Search

API search sends the search query to a server and displays the results. This is necessary for large datasets.

Debouncing: Search as You Type

Debouncing prevents making an API call on every keystroke. Instead, it waits for the user to stop typing (e.g., 500ms) before performing the search.

⚡ Why Debounce?

  • Prevents unnecessary API calls
  • Reduces server load
  • Improves performance and battery life
  • Better user experience (no constant loading)
Step-by-Step Explanation
1.
Choose the search approach – Decide between local filtering, API search, or both.
2.
Create the search UI – Add a TextField with a search icon and clear button.
3.
Implement the search logic – For local search, filter the list. For API search, make a network request.
4.
Add debouncing – Use a Timer to delay the search until the user stops typing.
5.
Handle states – Show loading, error, empty, and results states appropriately.
Search Best Practices

✅ Do's

  • Always add a clear button to the search field
  • Show loading indicators while searching
  • Display search results in a clear, scannable format
  • Show a "no results" message with helpful suggestions
  • Debounce API searches (300-500ms delay)
  • Handle errors gracefully with retry options
  • Highlight matching text in results (optional)

❌ Don'ts

  • Don't search on every keystroke without debouncing
  • Don't show raw JSON or error messages
  • Don't forget to handle empty searches
  • Don't block the UI while searching
  • Don't ignore the search input when it's empty
Common Mistakes
❌ Mistake 1: Not debouncing API searches

Without debouncing, every keystroke triggers an API call, causing performance issues and server load.

✅ Correct: Always debounce API searches

Use a Timer with a 300-500ms delay before performing the search.

❌ Mistake 2: Not handling empty search

If the user clears the search field, the app should return to the default state (show all items).

✅ Correct: Handle empty search

When the search query is empty, reset the results to show all items or show a placeholder.

❌ Mistake 3: Not cancelling previous requests

If a search takes a long time and the user types another query, the old request should be cancelled.

✅ Correct: Cancel in-flight requests

Use a CancelToken or check if the search term has changed before processing results.

🎯 Key Takeaway

Search is a critical feature for apps with large datasets. Use local filtering for small datasets and API search for large ones. Always debounce API searches to prevent unnecessary requests. Handle all states – loading, error, empty, and results. A well-designed search enhances the user experience significantly.