Objective
- Implement a feature that allows users to view other people’s Goals and Do0ne items.
- Provide a Like feature to cheer for each Goal.
Implementation Plan
- Add a Discover menu to display other users’ Goals and Do0ne lists.
- Add a field in each Goal document to store the number of Likes.
- Create a Likes subcollection under each Goal to track which users pressed Like.
- Restrict Likes to once per day per user.
- Add a Likers subcollection to record each user’s Like history (when and how many times).
Problem
- If one Goal has 20 Do0ne items and a user has 100 Goals, one full read requires 21 × 100 = 2,100 Firestore Reads.
- If 10 users perform this action, it results in 21,000 Reads, which is too costly.
Solution
- Run a Cloud Function on a schedule to generate snapshot JSON files containing Goals and Do0ne data.
- FlutterFlow will load these JSON snapshots via API calls instead of reading directly from Firestore.
- This reduces Firestore Reads since users fetch pre-generated JSON instead of live documents.
- Some information still requires real-time updates:
- Like count and whether the current user has Liked must be up-to-date.
- Therefore, Firestore is read only when a Goal is displayed to fetch its latest Like data.
FlutterFlow Implementation
- Create an API call to load the snapshot JSON file.
- Bind the API response to a PageView, generating Goal pages dynamically.
- When a PageView page becomes active, fetch the latest Like data from Firestore for that Goal.
- Implement Like / Unlike functionality.
Actual Implementation
- Fetching Goal data only when a PageView becomes active didn’t work reliably, so the entire dataset is now loaded when the PageView is first created.
- This eliminated the Read-saving advantage, so pagination was introduced when generating snapshots: each JSON file contains only 10 Goals.
- When the user reaches the last PageView page, the app requests the next snapshot API and appends the new pages dynamically.
View Likes on my Goal from others
Discover lets you see others’ Goals and Do0ne and cheer them on with Likes
Top comments (0)