Contents

Flutter and Android development with Kotlin

Widget cost estimator
Coding With Dhrumil
Tools
All articles→

Flutter Search Bar With ListView: Fast, Debounced Filtering

Build a Flutter search bar that filters a ListView smoothly: debouncing, lowercase matching, empty states, and keeping rebuilds cheap as data grows.

Search bar with magnifying glass icon above a list on a screen
Search bar with magnifying glass icon above a list on a screen · Foto: geralt / Pixabay

A search bar over a ListView looks trivial until the list gets real: a few thousand rows, a network call, and a keyboard that fires a rebuild on every keystroke. This guide walks through a search field that filters a list correctly the first time, with the small details that decide whether it feels instant or janky on mid-range Android devices.

Everything here uses core Flutter widgets — TextField, a controller, and a filtered list passed to ListView.builder. No packages required, and the same structure scales when the data later comes from an API instead of a local list.

Key points

  • Keep the unfiltered source list separate and derive the display list every build.
  • Debounce around 250–350 ms once filtering triggers network work.
  • Design the zero-results state explicitly; a blank screen reads as a bug.
  • Profile before optimising — rendering usually costs more than filtering.

The minimal working setup

Start with a StatefulWidget that owns two things: a TextEditingController and the original, unfiltered list. Never overwrite the source list when filtering — keep the full data separate and derive a second list for display. Filtering against the mutated list is the single most common bug here, because results shrink permanently and can never come back once the query is cleared.

The TextField gets an onChanged callback that calls setState with the new query. The body then renders ListView.builder with the derived list. Keeping itemBuilder lazy matters even at this stage: a ListView with children: built eagerly will construct every row on each keystroke, which is where the first visible lag appears once the list passes a few hundred items.

  • Store the full list separately from the filtered result
  • Use ListView.builder, not a children: array
  • Give every row a stable ValueKey
  • Clear the query by disposing the controller properly in dispose()
100 rows0.21,000 rows1.55,000 rows610,000 rows1250,000 rows55
Relative time spent per keystroke by list size (local filter) (ms)

Debounce typing before you filter or fetch

Filtering a local list of a few thousand strings is cheap enough to run on every keystroke, typically well under a frame. The moment filtering triggers a network call, per-keystroke work is wasteful and can return out-of-order responses. A debounce of 250 to 350 ms matches how fast most people type and feels responsive without hammering the backend; below 150 ms you barely filter anything, above 500 ms the field feels laggy.

Implement it with a Timer stored on the state, cancelled and restarted on each change. If you do fetch, also keep a request counter or compare the query when the response arrives, and drop stale results. In practice, one line — ignoring a response whose query no longer matches the current text — removes most flicker and wrong-result bugs in search screens.

  • 250–350 ms is a good default debounce window
  • Cancel the previous Timer on every new keystroke
  • Discard responses that do not match the current query
  • Show a small progress indicator only after ~300 ms of loading

Matching rules that behave the way users expect

Case-insensitive substring matching covers the overwhelming majority of search bars. Lowercase both sides with toLowerCase() and use contains(), which is simple and predictable. If your data is Turkish, German, or other locales with special casing, use toLowerCase() carefully and test dotted and dotless i — Dart follows Unicode rules, and users notice when 'I' stops matching 'i'.

For multi-word queries, split the query on whitespace and require every token to match somewhere in the row, combining name, description and tags into one lowercase haystack. That handles 'john flutter' matching a row named John with a flutter tag. Avoid regex for user input unless you escape it; an unescaped '(' in the field throws a FormatException and crashes the filter.

Empty states, focus and keyboard behaviour

Three states need distinct UI: no query (show everything), query with results, and query with zero results. The zero-results state should name the query — 'No matches for "kotlin"' — so users understand the field is working. Without it, a blank screen reads as a bug and people retype the same thing.

Set textInputAction to TextInputAction.search and handle onSubmitted to unfocus the field, which hides the keyboard on Android and gives the list the full screen back. Add a clear (X) suffix icon that appears only when text is non-empty, and call controller.clear() plus setState with an empty query. On iOS keep autocorrect off for search fields; silent corrections create queries the user never typed.

  • Distinct UI for no query, results, and no results
  • textInputAction: TextInputAction.search with onSubmitted unfocus
  • Conditional clear button that resets both controller and query
  • autocorrect: false so the query stays exactly as typed
Approach comparison for a Flutter search bar over a list
ApproachTypical latencyBest forMain risk
Filter on every keystroke (local)Under 16 ms for a few thousand rowsSmall to medium local listsWasted work on large data
Debounced local filter250–350 ms after typing stopsLarge local lists, expensive rowsFeels slow if window is too long
Debounced remote searchNetwork latency plus 250–350 msServer-side dataOut-of-order responses
Local filter plus remote refreshInstant, then replacedOffline-friendly searchResult flicker if not merged carefully

Keeping rebuilds cheap as the list grows

The whole screen rebuilding on each keystroke is fine for small lists, but you can narrow it by extracting the results into their own widget that takes the filtered list as a parameter. That way the TextField subtree is not rebuilt unnecessarily. Use const constructors on row widgets where possible; Flutter can then skip rebuilding unchanged subtrees entirely, which shows up as smoother typing on low-end hardware.

Row layout matters as much as filtering. A row with an Image.network, a Column of three Texts and a nested ListView will be the bottleneck long before the filter is. Keep rows flat, size images explicitly with cacheWidth, and avoid heavy shadows or clip widgets inside itemBuilder. If your list rows start getting complicated, the same sizing and recycling advice from native lists applies to how you structure the widget tree.

  • Extract the results list into its own widget
  • Prefer const constructors and flat row trees
  • Set cacheWidth on network images
  • Profile with DevTools timeline before optimising blindly

When search grows beyond a local filter

Past roughly 10,000 rows, linear filtering still finishes in a few milliseconds on modern phones, so do not over-engineer. But when the data lives on a server, you shift to a two-stage model: filter locally on whatever is already loaded for instant feedback, then replace results when the debounced request returns. This keeps the field feeling immediate even on a slow connection.

For very large local datasets, a prefix index (a Map from first two or three lowercase characters to row indices) cuts the candidate set before contains() runs, but only bother if profiling shows the filter itself is the cost. Usually the bottleneck is rendering. The same discipline of measuring before optimising applies to list performance work on the Android side, where adapter sizing decisions have an outsized effect on scroll smoothness.

Testing the search bar

Widget tests catch the regressions that matter here: entering text updates the visible rows, clearing restores the full list, and an unmatched query shows the empty state. Use tester.enterText on the field's finder and pump once; with a debounce, pump a Duration slightly longer than the timer to let it fire. Without that, tests pass or fail depending on timing and become flaky in CI.

For the fetch path, inject the client so tests return fixed data and assert that stale responses are dropped. A single test that types 'a', then 'ab', then completes the 'a' response late and expects 'ab' results is worth more than ten snapshot tests. Keep the filter function pure and unit-test it directly with edge cases: empty query, mixed case, extra spaces, and special characters.

  • Test enterText, pumped past the debounce duration
  • Assert the clear button restores the unfiltered list
  • Unit-test the pure filter function with odd inputs
  • Cover the stale-response case explicitly
Dhrumil Patel

Editor and Flutter contributor. Writes practical Flutter tutorials focused on widgets, layout and animation. Enjoys breaking large UI problems into small, testable pieces and explaining them clearly to beginners.

Flutter Screen Widget and Cost Estimator

Related articles

Cookie preferences

Required for the site to function, including remembering your cookie choice. Always on.

Not used on this site.

Not used on this site.