Photo Social App with FlutterFlow and Supabase · Course book
Chapter 22: Replies and Recursion
Conversations branch. Someone comments on a photo, a friend replies, and the original commenter replies to the reply. A flat list of comments loses that structure, so this part adds replies, including replies to replies, using a powerful idea: a recursive component, a comment component that contains a list of comment components. This chapter explains how replies are stored as a tree with a parent id, how recursion displays a tree of any depth and why it always stops, how to hide and reveal replies, how to add a reply in the right place, the indentation limits worked out by hand with a graph, and the debugging and testing that nested data needs.
Replies as a tree
The comments table already has an optional parent_id column. A top-level comment leaves it empty; a reply stores the id of the comment it answers. That single self-referencing column turns the table into a tree: each comment can have any number of children, each child can have its own children, and following parent_id upward from any reply always leads back to one top-level comment.
| id | parent_id | author | content | depth |
|---|---|---|---|---|
| 1 | empty | maya | Where was this taken? | 0 |
| 2 | 1 | alex | Lisbon, at sunset | 1 |
| 3 | 2 | maya | Adding it to my list! | 2 |
| 4 | 1 | sam | I was there last year | 1 |
| 5 | empty | leo | Beautiful colours | 0 |
The comment section queries only top-level comments, those whose parent_id is empty, filtered by the post. Each comment, when its replies are shown, queries the comments whose parent_id equals its own id. An index on parent_id keeps those queries fast. No extra table is needed, and replies of any depth fit the same structure.
The depth of a comment is the number of steps from it up to its top-level ancestor: zero for a top-level comment, one for a direct reply, two for a reply to a reply. The table does not store depth, because it can always be worked out by following parents, but a component can track it simply: the section passes depth zero to each top-level comment, and each comment passes its own depth plus one to its replies. That number then drives the indentation and any depth limits, without a single extra query.
Fetching a whole thread at once is also possible, using a recursive query in SQL, a query written with with recursive that starts from a top-level comment and repeatedly adds the replies of the rows found so far until no more appear. Wrapped in a database function, it returns an entire tree in one request, with each row's depth calculated. For this app, loading replies level by level as threads are opened is simpler and usually loads less data, but the recursive query is worth knowing for screens that must show a full thread immediately.
Recursive components
A component is recursive when it contains itself. The comment component shows one comment, and beneath it, a list whose items are comment components, each showing one reply, and each able to show its own replies the same way. In FlutterFlow, the comment component's reply list has a backend query for comments with parent_id equal to the current comment's id, and generates a comment component for each result.
Every recursion needs a way to stop, or it would go on forever. Here the data provides it: a comment with no replies returns an empty query, so its reply list contains nothing and no further components are built. The tree in the table above therefore produces exactly five components, nested to the depth of the conversation, and no more. Because replies are hidden until requested, as described next, components are also only built when someone actually opens a thread, which keeps a busy post fast even with hundreds of replies.
Hiding and showing replies
Showing every reply of every comment at once makes a busy section overwhelming, and loads far more data than people want. The usual pattern hides replies behind a line such as "View 2 replies" under each comment that has them. Tapping it reveals the replies and changes the line to "Hide replies". Whether a comment's replies are visible is a piece of component state, a true-or-false value belonging to that one comment, so expanding one thread leaves the others closed.
The reply list's query should only run when the replies are visible, which FlutterFlow can arrange by placing the list inside a widget that is shown conditionally. The count in "View 2 replies" needs the number of replies without loading them; a view that adds a reply count to each comment, or a stored count kept by a trigger like the like count, provides it cheaply. Comments with no replies show no line at all.
Long threads can be revealed in steps. Rather than loading all 80 replies to a popular comment when "View replies" is tapped, the first tap can load the oldest five, with "View 75 more replies" beneath them, and each further tap loads the next batch. This keeps the first reveal instant, limits the data loaded to what people actually read, and keeps the comment section scrolling smoothly. When a thread is hidden again, its replies can stay cached in the component, so reopening it is instant.
Adding a reply
Tapping "Reply" under a comment tells the input bar that the next comment is a reply. The comment component calls an action parameter supplied by the section, passing its own id and author; the section stores the id in its state as the reply target, shows a small banner above the input bar, "Replying to @maya", with a cross to cancel, and focuses the text field. Posting then inserts a row with parent_id set to the stored id, then clears the target, refreshes that comment's replies and makes sure they are visible so the new reply can be seen.
Replying to a reply works in exactly the same way, because a reply is a comment like any other. Many apps simplify the display by attaching deep replies to the second level, so that a reply to a reply appears alongside its parent rather than one level deeper, with the "@name" mention showing who is being answered. That keeps threads readable on narrow screens, as the next calculation shows. Even when the display is flattened, the database keeps the true parent, so the structure is never lost and a future design could show it differently.
How deep can replies go?
Each level of nesting is usually indented, so the reader can see what answers what. Indentation takes width, and a phone has little to spare.
Debugging and testing
Recursive layouts produce characteristic bugs. If replies appear under every comment, the reply query is filtering by the post instead of by parent_id. If a new reply appears as a top-level comment, the reply target was not passed to the insert. If expanding one thread expands them all, the visibility value is stored in page state rather than in each comment's component state. And if the app becomes slow on a popular post, reply lists are probably loading before they are shown. Testing should build a small tree with two accounts, replying at several depths, then check the structure in the comments table, the indentation on screen, hiding and showing threads, cancelling a reply, and deleting a comment that has replies to see whether the chosen rule, removing or keeping the thread, works as intended.
Practice tasks
- Using the table in this chapter, list the ids that a query for replies to comment 1 returns, and the depth of comment 3.
- With 20 points of indentation per level and 292 points at depth 0, how much text width remains at depth 3 and at depth 6?
- Explain what stops the recursive comment component from building components for ever.
- Why must the "show replies" value live in component state?
- Describe the steps from tapping "Reply" to the new reply appearing under its parent.
- Give one reason to cap visual indentation, and describe how deep replies are shown after the cap.
This course book is available to read online only at tech-academy.chrysolites.org.