Photo Social App with FlutterFlow and Supabase · Course book
Chapter 21: Comments in Supabase
The comment section looks complete, but until now it has shown placeholder comments and its Post button has done nothing. This chapter connects it to Supabase. It designs the comments table, writes the action that posts a comment, displays a post's comments with their authors in the right order, formats each comment's age as a short relative time, worked out by hand, walks through the debugging that connecting a list usually needs, and tests the finished feature with more than one account. By the end, people can hold real conversations under every photo.
The comments table
A comment belongs to one post and one author, so the comments table holds two foreign keys alongside the text. A third, optional foreign key, pointing to another comment, will make replies possible in the next part; adding it now saves changing the table later.
| Column | Type | Rule | Purpose |
|---|---|---|---|
| id | int8 or uuid | Primary key | Identifies the comment |
| post_id | matches posts.id | Foreign key; required; cascade on delete | The post being discussed |
| user_id | uuid | Foreign key to users; required | The author |
| content | text | Required; check length between 1 and 1,000 | The comment itself |
| parent_id | matches comments.id | Optional; foreign key to comments | Set for replies, empty for top-level comments |
| created_at | timestamptz | Default now() | Ordering and relative times |
Cascading deletes from posts mean that deleting a post removes its whole conversation. Row level security policies should let signed-in users read comments, insert comments only with their own user_id, and delete only their own comments; many apps also let a post's owner delete comments on their post, which a policy can allow by checking the post's author. An index on post_id keeps loading a post's comments fast as the table grows, and after creating the table the schema must be refreshed in FlutterFlow.
A view that joins comments with their authors, returning each comment with the author's username and avatar, saves the comment component from fetching user details separately, exactly as the posts view did for the feed. The section's list then queries that view.
The check constraint on the content column deserves a word. Requiring at least one character stops empty comments even if the app's own check is bypassed, and a maximum such as 1,000 characters prevents someone pasting an enormous block of text that would break the layout and fill the database. Constraints like these cost nothing to enforce and protect every client, including future versions of the app and any scripts that write to the table.
Comments are public speech, so the table should also be ready for moderation. App stores expect apps with user content to let people report abusive comments and block users, and to remove content that breaks the rules. A simple reports table, holding the comment's id, the reporter and a reason, and a policy that hides comments from blocked users, can be added alongside the comments table. Even if these features come later, designing the comments table with stable ids and clear authorship makes them straightforward to add.
Posting a comment
The input bar's Post button runs a short chain of actions. First, check that the trimmed text is not empty; if it is, do nothing. Second, run an Insert Row action on the comments table with the post's id, the authenticated user's id and the text. Third, if the insert succeeded, clear the text field, dismiss the keyboard if appropriate, and refresh the comment list so the new comment appears. If the insert failed, keep the text in the field and show a short message, so that nothing the user wrote is lost.
Two refinements make posting feel immediate. Disabling the Post button while the insert runs prevents a double tap from posting the same comment twice. And scrolling the list to the new comment after refreshing shows the user that it worked. Where comments appear depends on the chosen order: newest first places it at the top; oldest first places it at the bottom, which then needs a scroll to the end. Whichever is chosen, the same order should apply in the feed preview and the full section, so a comment the user has just read in one place is easy to find in the other.
Displaying comments
The comment section's ListView gets a backend query on the comments-with-authors view, filtered to the current post's id and, for now, to top-level comments where parent_id is empty. Each generated comment component receives its row and shows the author's avatar, the username and text as rich text, and the details line. Ordering is a design choice: oldest first reads like a conversation from the beginning, while newest first surfaces recent activity. Many photo apps show the most relevant or most liked comments first, which needs a like count per comment and is a natural later improvement.
Long conversations should load in pages, perhaps 20 comments at a time with more fetched as the user scrolls, so a post with thousands of comments opens as quickly as one with three. The post card in the feed can show a short preview, the first one or two comments and a "View all 24 comments" link that opens the full section, using a count from the database rather than downloading every comment just to count them.
Each comment's time is shown as a short relative time, which is easier to read than a full date. The rule picks the largest unit that fits.
FlutterFlow can format dates relative to now, or a small custom function can apply exactly these rules. Relative times go stale while a sheet stays open, so refreshing the list when the sheet reopens keeps them accurate enough.
Each comment row can also offer actions through a long press or a small menu: delete for the comment's author, report for everyone else, and copy text. Showing "Delete" only on the viewer's own comments requires comparing the comment's user_id with the signed-in user's id, and the delete policy in the database enforces the same rule. Deleting should ask for confirmation, then remove the row and refresh the list. If replies are added later, deleting a comment that has replies raises a question worth deciding now: remove the whole thread, or keep the replies and show the parent as "Comment deleted".
Debugging and testing
Connecting a list to new data usually needs some debugging, and the symptoms point to the cause. If posting seems to work but nothing appears, check the list's query: is it filtered by the right post id, and is the view's schema refreshed in FlutterFlow? If inserting fails, check the insert policy and that the post id passed into the component is not empty. If every post shows the same comments, the filter is probably using a fixed value from testing rather than the current post's id. If new comments appear only after closing and reopening the sheet, the list is not being refreshed after the insert. Looking at the comments table in the Supabase editor while posting shows immediately whether rows are being written.
A good test uses two accounts: comment from one, then open the same post on the other and check the comment appears with the right author, avatar and time. Post a very long comment, an emoji-only comment and a comment containing line breaks to check the layout. Delete a post and confirm its comments disappear from the table. Try commenting with no network to see the failure message, and check that the text is kept. With comments working, the next part adds replies, so conversations can branch.
Practice tasks
- It is 18:00 on 20 November. Give the relative time for comments posted at 17:58:10, 16:20, 06:45 the same day, and 09:00 on 15 November.
- Write the filter for the list query that shows only top-level comments on the current post.
- Write, in words, the delete policy that lets authors delete their comments and post owners delete comments on their posts.
- Every post shows the same three comments. What is the likely cause?
- Why should the Post button be disabled while the insert runs?
- Explain why the feed's "View all 24 comments" link should use a count from the database rather than downloading the comments.
This course book is available to read online only at tech-academy.chrysolites.org.