Photo Social App with FlutterFlow and Supabase · Course book
Chapter 15: Searching Posts
The search page has looked finished for a while, but typing into it has done nothing. This chapter makes it work with the quickest method FlutterFlow offers: fetch posts with a Query Rows action, then filter them on the phone with the built-in Simple Search action, which matches text even when the user makes small typing mistakes. It explains how the two actions fit together, how fuzzy matching decides that "sunst" should find "sunset", worked out by hand with edit distance, how the results are shown in the grid, how to test the feature, and where this approach stops scaling, which motivates the server-side search built in the next part.
Two steps: fetch, then search
Simple Search works on a list the app already has. It does not ask the database anything; it looks through a list of rows held in the app and returns those whose chosen text fields match the search term. So the search page needs two actions in order.
- Query Rows: when the page loads, fetch the posts to search, for example from the posts-with-authors view, and store the result in page state.
- Simple Search: when the user submits a search, search that stored list, comparing the term with fields such as the caption and the username, and store the matches in another page state variable.
The grid of results is then bound to the matches. Before any search runs, the grid can show the full list, acting as the explore view; after a search, it shows only what matched. Clearing the search box resets the grid to the full list.
| Piece | Where it runs | What it produces |
|---|---|---|
| Query Rows action | The database, once when the page opens | A list of post rows, kept in page state |
| Search text field | The phone | The term the user typed |
| Simple Search action | The phone, on each search | The rows that match, best first |
| Results grid | The phone | Thumbnails of the matching posts |
The search can run when the user presses the keyboard's search key, or as they type. Searching as they type feels responsive, and because Simple Search works on data already on the phone, it costs no network requests, so it can run on every change of the text without waiting.
Fuzzy matching and edit distance
People misspell searches all the time, and on phone keyboards especially. Exact matching would find nothing for "coffe" even when dozens of captions say "coffee". Simple Search is fuzzy: it scores how similar the term is to each field and returns rows that are close enough, ranked by score, so near-misses still find what the user meant.
A standard measure of similarity between two words is the edit distance, also called Levenshtein distance: the smallest number of single-character insertions, deletions or substitutions needed to turn one word into the other. The smaller the distance, the closer the words.
Simple Search uses a fuzzy-matching library with its own scoring, which also considers where in a field the match occurs, so the exact numbers differ, but the idea is the same: closer strings score better. Matching is usually case-insensitive, so "Sunset" and "sunset" are treated alike.
Two choices shape how well the search behaves. The first is which fields to search. Searching the caption and the author's username covers most of what people look for in a photo app; adding a location field later would let "Lisbon" find posts tagged there. Searching too many fields, such as long descriptions, tends to produce surprising matches that are hard for users to understand. The second is how strict the matching is. Fuzzy search libraries usually offer a threshold: a strict threshold returns only close matches, so a typo of two letters might find nothing; a loose one returns more results, including some that look unrelated. Trying a handful of real searches with typical typos, and adjusting until the first few results feel right, is the practical way to choose.
Ordering matters as much as matching. Because fuzzy search ranks results by score, the best match appears first, but two posts with similar scores can appear in either order. Breaking ties by recency, so that newer posts come first among equally good matches, makes the results feel fresh. Showing at most the first few dozen results, with the rest available by scrolling, keeps the grid quick to draw.
Search results should also respect the same rules as the feed. If users can block accounts or hide posts, those rows must be excluded from the list that is searched, not just from the feed; otherwise search becomes a way to see content that was meant to be hidden. Since Simple Search only looks at what the Query Rows action fetched, filtering that query correctly is enough.
Reflecting the results
The results grid's children come from the matches stored in page state, each tile showing a post's thumbnail and opening the post when tapped. Three states need designing. With no search term, show the full fetched list. With a term and matches, show the matches, best first. With a term and no matches, show a friendly empty state: "No posts found for 'sunst'". The search box's clear button empties the term and restores the full list, and the keyboard should close when the user starts scrolling the results.
The accounts tab can work the same way, fetching user rows and searching the username and display name fields. Because each tab keeps its own list and matches, switching tabs keeps both result sets ready. Running both searches from the same text field means one term searches posts and accounts at once.
Testing, and where this approach stops
Testing should include exact matches, misspellings one or two letters off, different capitalisation, partial words, an empty search and a search with no matches. Check that tapping a result opens the right post, and that clearing the search restores the full grid without stale results.
The weakness of Simple Search is that it can only search what has been downloaded. If the page fetches the newest 200 posts, a post from last year can never be found. Fetching every post instead would make the page slow and wasteful as the app grows.
For a small app, or for searching a short list such as a user's own followers, Simple Search is perfect: instant, forgiving and free of extra setup. For searching all posts in a growing app, the search must move to the database, where an index can find matches among millions of rows. The next part builds exactly that, calling Supabase through an API call so that the database does the searching and returns only the results.
Recap
This part fetched posts with a Query Rows action, searched them on the phone with Simple Search's fuzzy matching, and displayed the matches in the results grid with sensible empty states. The approach is quick to build and pleasant to use, and its one limit, searching only what was downloaded, is clear from the numbers above.
Practice tasks
- Compute the edit distance between "flowr" and "flower", "bridje" and "bridge", and "cat" and "act".
- With a rule of at most one edit for terms under six letters, which of these match "beach": "beech", "bench", "peach", "reach", "breach"?
- How much data would downloading 8,000 post rows of 400 bytes each require?
- Describe the three states of the results grid and what each shows.
- Why does Simple Search cost no network requests while the user types?
- Give one situation where Simple Search is the right tool and one where a server-side search is needed.
This course book is available to read online only at tech-academy.chrysolites.org.