I am implementing similar app and requirement is to save user's rating
and review
in firebase database. I am following this project and blog you wrote and it has helped me to make inroads. However, I am a bit confused on how to add new entries for a particular user in the db and looking for your help / input. My current code is blow:
Model:
class Review {
final int rating;
final String review;
final String created;
final String fullName;
final String city;
final String state;
final String profilePicURL;
Review(
{this.rating,
this.review,
this.created,
this.fullName,
this.city,
this.state,
this.profilePicURL});
factory Review.fromJSON(List list, int index) {
return Review(
rating: list[index]['Rating'] as int,
review: list[index]['Review'] as String,
created: list[index]['Created'] as String,
fullName: list[index]['FullName'] as String,
city: list[index]['City'] as String,
state: list[index]['State'] as String,
profilePicURL: list[index]['profile_pic'] as String,
);
}
Firebase structure:
I am able to read the reviews
data from db as below:
@override
void initState() {
super.initState();
final FirebaseDatabase database = new FirebaseDatabase();
databaseReference = database.reference().child('reviews');
FirebaseAuth.instance.currentUser().then((user) {
proId = pro.uid;
print(proId);
// databaseReference.push().set(proId);
});
databaseReference.orderByChild('rating').once().then((DataSnapshot snapshot) {
print('${snapshot.value}');
});
}
proId
is the current user's id which is returned correctly. I want to store the rating that user selects whose code is:
StarRating(
starCount: 5,
starSize: 36,
color: Colors.yellow,
rating: rating,
onRatingChanged: (v) {
rating = v;
setState(() {
Review review;
if (review != null) {
databaseReference.push().set(rating);
}
});
},
)
With above code, however, the data is not being saved correctly in the db:
Looking for guidance / help on how to correctly store the rating
in the db at correct path reviews/{proid}/{rating}