Search through all our blogs and become a FlutterFlow expert in no time! We cover all things from beginner to expoert.
In today’s app-driven world, rating systems play a crucial role in determining user satisfaction and building trust. Whether you’re running a food delivery app, an e-commerce store, or a marketplace, a well-implemented rating system ensures transparency and helps users make informed decisions.
In this blog, we’ll walk through the steps to create a comprehensive rating system using Firebase, complete with a setup for collecting reviews, subcollections, and a Cloud Function that updates an average rating in the parent collection.
Let’s say we’re building a restaurant review app. Users will be able to leave reviews and rate their dining experience. Each restaurant will have a subcollection for reviews, and the ratings will be averaged using a Cloud Function that updates the parent restaurant collection.
Restaurants collection:
restaurants/{restaurantId}
)name
: The name of the restaurantlocation
: Location of the restaurantaverage_rating
: Average rating (to be updated based on reviews)total_reviews
: The total number of reviewsSubcollection for reviews:
reviews
subcollection (e.g., restaurants/{restaurantId}/reviews
)user_id
: The ID of the user leaving the reviewrating
: The rating given (e.g., 1–5 stars)comment
: Optional text feedback from the usertimestamp
: The time the review was createdThis simple setup provides flexibility to manage user reviews without overloading the main restaurant collection.
To set up the subcollection for reviews in FlutterFlow:
reviews
subcollection under the restaurant collection. Each time a user submits a review, a new document will be created in this subcollection with the fields mentioned above.reviews
subcollection for the respective restaurant.{
"user_id": "user123",
"rating": 4,
"comment": "Great food, but the service was slow.",
"timestamp": "2024-10-01T10:15:00"
}
The core part of this rating system is calculating the average rating for each restaurant whenever a new review is added. We’ll set up a Cloud Function to listen to new documents in the reviews
subcollection and update the average_rating
and total_reviews
fields in the parent restaurants
collection.
const functions = require("firebase-functions");
const admin = require("firebase-admin");
exports.updateRestaurantRating = functions.firestore.document('restaurants/{restaurantId}/reviews/{reviewId}')
.onCreate(async (snap, context) => {
// Get the restaurant ID
const restaurantId = context.params.restaurantId;
// Get the new review's rating
const newRating = snap.data().rating;
// Reference to the parent restaurant document
const restaurantRef = admin.firestore().collection('restaurants').doc(restaurantId);
try {
// Get the current restaurant data
const restaurantDoc = await restaurantRef.get();
const restaurantData = restaurantDoc.data();
// Get the current average rating and total reviews
const currentAverage = restaurantData.average_rating || 0;
const totalReviews = restaurantData.total_reviews || 0;
// Calculate the new average rating
const updatedTotalReviews = totalReviews + 1;
const updatedAverageRating = ((currentAverage * totalReviews) + newRating) / updatedTotalReviews;
// Update the restaurant document
await restaurantRef.update({average_rating: updatedAverageRating, total_reviews: updatedTotalReviews});
console.log(`Restaurant ${restaurantId} updated with new average rating: ${updatedAverageRating}`);
} catch (error) {
console.error('Error updating restaurant rating:', error);
}
});
reviews
subcollection of a restaurant.restaurant
document with the new average rating and the updated total number of reviews.Once the average rating is calculated and stored in the average_rating
field of the restaurant document, you can display it in your FlutterFlow app on the restaurant profile page. Use a simple query to fetch the restaurant data and bind the average_rating
field to your UI.
You can also style the rating with stars or any custom design using FlutterFlow’s widgets (or build your own).
If you'd like to extend the rating system further, here are some additional features you can implement:
By following these steps, you’ll have a robust rating system that calculates average ratings in real-time, ensuring your users have an accurate reflection of each restaurant’s performance. The combination of Firebase and FlutterFlow makes this process straightforward and scalable.
And if you're looking to implement a rating system like this without building it from scratch, you can reach out to us and we'll help you build it!
Sed at tellus, pharetra lacus, aenean risus non nisl ultricies commodo diam aliquet arcu enim eu leo porttitor habitasse adipiscing porttitor varius ultricies facilisis viverra lacus neque.