Photo Social App with FlutterFlow and Supabase · Course book
Chapter 18: Likes
The heart under each photo is the smallest and most-used interaction in the app. Making it work properly means storing who liked what, letting each person like a post only once, showing a filled heart on posts the viewer has already liked, and updating instantly when tapped. This chapter builds likes in FlutterFlow and Supabase: designing the likes table as a link between users and posts, using FlutterFlow's toggle icon widget for the heart, writing the like and unlike actions, storing the liked state so the heart is correct everywhere, debugging the usual problems, and testing. It also estimates by hand how large a likes table becomes.
The likes table
A like is a relationship between a user and a post, and since a user can like many posts and a post can be liked by many users, it is a many-to-many relationship. Such relationships are stored in an intermediate table, sometimes called a join table, whose rows each hold one pair of foreign keys.
| Column | Type | Rule |
|---|---|---|
| id | int8 or uuid | Primary key |
| user_id | uuid | Foreign key to users; required |
| post_id | int8 or uuid | Foreign key to posts; required; cascade on delete |
| created_at | timestamptz | Default now() |
| (user_id, post_id) | – | Unique together: one like per user per post |
The unique constraint on the pair of columns is the rule that makes likes trustworthy: whatever the app does, the database refuses a second like from the same user on the same post. Cascading deletes from posts mean that deleting a post removes its likes, so no orphaned rows remain in the table. Row level security policies should let signed-in users read likes, insert likes only with their own user_id, and delete only their own likes.
post_id and user_id, and the unique index on the pair, are what keep likes queries fast.The toggle icon
FlutterFlow's ToggleIcon widget shows one of two icons depending on a true-or-false value: an outlined heart when the value is false and a filled red heart when it is true. Tapping it flips the value and runs an action. That is exactly the behaviour of a like button, so the post card's plain heart icon is replaced with a toggle icon whose value means "the viewer has liked this post".
The toggle icon needs two things: a source for its current value and an action for when it is tapped. The value comes from the liked state, described below. The action checks the new value: if the heart has just become filled, insert a like; if it has just become empty, delete the viewer's like for this post. Colour and size come from the theme, with a filled red heart and a slightly larger size giving clear feedback.
A toggle icon changes its appearance the moment it is tapped, before the database has replied. That is an optimistic update: the app assumes the request will succeed, because it almost always does, and corrects itself if it does not. The correction matters. If the insert fails, perhaps because the network dropped, the heart should return to empty and the liked list should drop the id again, ideally with a brief message such as "Couldn't like this post". Without that step, the heart shows a like that the database never recorded, and the next reload contradicts what the user saw. FlutterFlow's conditional actions, checking whether the insert succeeded, make the rollback straightforward.
Many photo apps also let people like a post by double-tapping the image, with a large heart that pops up and fades. In FlutterFlow, a double-tap gesture on the image can run the same like action and briefly show an animated heart widget. Double-tap should only ever like, never unlike, because people double-tap enthusiastically and would be surprised to remove a like they meant to add; unliking stays on the heart icon.
Accessibility needs attention too. A heart icon alone means nothing to a screen reader, so the toggle should carry a label such as "Like" or "Unlike" according to its state, letting blind and partially sighted users like posts as easily as anyone else.
The like and unlike actions
Liking is an Insert Row action on the likes table with the authenticated user's id and the post's id. Unliking is a Delete Row action on the likes table, filtered to rows where user_id equals the viewer and post_id equals this post; because of the unique constraint, that matches at most one row. Both actions run on tap, and their success or failure can be checked so that the interface can be corrected if something goes wrong.
Double taps and slow networks can send an insert twice. With the unique constraint in place, the second insert fails harmlessly, and the app can ignore that particular error. Without it, the table could quietly collect duplicate likes and every count would be wrong, which is why the constraint belongs in the database from the start.
Storing the liked state
The heart must be filled on posts the viewer has already liked, on every screen. Asking the database separately for each post, "has this user liked post 17?", would cost one request per post in the feed. A more efficient approach loads the ids of all posts the viewer has liked once, after sign-in or when the feed loads, and keeps them in app state as a list. Each toggle icon's value is then "is this post's id in the list?", a check that happens instantly on the phone.
When the viewer likes a post, the action adds its id to the list as well as inserting the row; when they unlike it, the action removes the id as well as deleting the row. Because every heart reads from the same list, liking a post on the detail page also fills its heart in the feed and on the profile grid without any further request. For a user who has liked a great many posts, the list could become large, at which point fetching only the likes for the posts currently on screen is the better design, but for most users a list of a few hundred or thousand ids is small.
The list must be cleared when the user logs out and reloaded when another user signs in, so that the next person never sees someone else's likes on a shared device. If the list is persisted between launches for speed, it should still be refreshed from the database when the app starts, so that likes made on another phone appear correctly here too.
Debugging and testing
Likes rarely work perfectly the first time, and the problems fall into recognisable patterns. If tapping the heart does nothing in the database, check the insert policy and that the schema was refreshed after creating the table. If the heart fills but reverts on reloading, the liked list is not being loaded from the database at start-up. If unliking fails, check the delete policy and the filters on the delete action. If the heart shows the wrong state after scrolling, the toggle's value is probably bound to page or component state instead of the shared list. Watching the likes table in the Supabase table editor while tapping is the quickest way to see what is really happening behind the heart.
A good test plan likes and unlikes a post several times quickly, checks the table has at most one row for that pair, likes from the detail page and checks the feed, logs out and in again to check the hearts are restored, and tries with two accounts to make sure each sees only their own hearts. With those checks passing, the next part adds a database trigger that keeps each post's like count correct automatically.
Practice tasks
- Estimate the size of a likes table for 40,000 posts with an average of 12 likes each, at 64 bytes per row.
- Write the filters for the delete action that removes the viewer's like on a post.
- Explain how the unique constraint makes a double tap harmless.
- Why is the list of liked post ids kept in app state rather than in each post card?
- Describe what must happen to the liked list when a user logs out.
- The heart fills when tapped but is empty again after restarting the app. What is the likely cause?
This course book is available to read online only at tech-academy.chrysolites.org.