Every time I send the rating, whether it is 1, 2, 3, 4, or 5, it is stored in the database with a value of 5. I have made many attempts to no avail. Please help.
this is my code Three pages are responsible for sending that value The first page sends the value to a function on the second page, and the second page sends the value to a function on the third page to send it to the database.
first page = > review
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_rating_bar/flutter_rating_bar.dart';
import 'package:get/get.dart';
import 'package:hallo_doctor_client/app/modules/widgets/submit_button.dart';
import '../controllers/review_controller.dart';
class ReviewView extends GetView<ReviewController> {
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => FocusManager.instance.primaryFocus?.unfocus(),
child: Scaffold(
appBar: AppBar(
title: Text('Review Doctor'.tr),
centerTitle: true,
),
body: SingleChildScrollView(
child: Container(
width: double.infinity,
padding: EdgeInsets.all(10),
child: Card(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ListTile(
leading: CircleAvatar(
radius: 40,
backgroundImage: CachedNetworkImageProvider(
controller.timeSlot.doctor!.doctorPicture!),
),
title: Text(controller.timeSlot.doctor!.doctorName!),
),
Divider(),
TextField(
controller: controller.textEditingReviewController,
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'Your Review'.tr),
maxLines: 10,
textInputAction: TextInputAction.done,
onSubmitted: (value) => controller.review = value,
),
Divider(),
Obx(() => RatingBar.builder(
initialRating: controller.rating.value,
minRating: 1,
direction: Axis.horizontal,
allowHalfRating: true,
itemCount: 5,
itemPadding: EdgeInsets.symmetric(horizontal: 4.0),
itemBuilder: (context, _) => Icon(
Icons.star,
color: Colors.amber,
),
onRatingUpdate: (rating) {
FocusManager.instance.primaryFocus?.unfocus();
print(rating);
},
)),
SizedBox(
height: 50,
),
SubmitButton(
onTap: () {
controller.saveReiew();
},
text: "Send".tr)
],
),
),
),
),
),
);
}
}
Please focus on that widget Because he is responsible
Obx(() => RatingBar.builder(
initialRating: controller.rating.value,
minRating: 1,
direction: Axis.horizontal,
allowHalfRating: true,
itemCount: 5,
itemPadding: EdgeInsets.symmetric(horizontal: 4.0),
itemBuilder: (context, _) => Icon(
Icons.star,
color: Colors.amber,
),
onRatingUpdate: (rating) {
FocusManager.instance.primaryFocus?.unfocus();
print(rating);
},
)),
the second page = > contrroler
import 'package:flutter/cupertino.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:hallo_doctor_client/app/models/time_slot_model.dart';
import 'package:hallo_doctor_client/app/service/review_service.dart';
import 'package:hallo_doctor_client/app/service/user_service.dart';
class ReviewController extends GetxController {
//TODO: Implement ReviewController
var review = '';
var rating = 5.0.obs;
TextEditingController textEditingReviewController = TextEditingController();
TimeSlot timeSlot = Get.arguments;
var price = 0.obs;
var ratings = '';
@override
void onInit() {
super.onInit();
price.value = timeSlot.price!;
}
@override
void onClose() {}
void saveReiew() async {
EasyLoading.show(maskType: EasyLoadingMaskType.black);
var user = UserService().currentUserFirebase;
try {
await ReviewService().saveReview(textEditingReviewController.text,
rating.value.toInt(), timeSlot, user!);
Get.close(2);
//Get.back();
} catch (e) {
Fluttertoast.showToast(msg: e.toString());
} finally {
EasyLoading.dismiss();
}
}
}
Third page service => Send the values to the database
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:hallo_doctor_client/app/models/doctor_model.dart';
import 'package:hallo_doctor_client/app/models/review_model.dart';
import 'package:hallo_doctor_client/app/models/time_slot_model.dart';
import 'package:hallo_doctor_client/app/service/user_service.dart';
class ReviewService {
Future saveReview(
String review, int rating, TimeSlot timeSlot, User user) async {
try {
await FirebaseFirestore.instance
.collection('Review')
.doc(timeSlot.timeSlotId)
.set({
'review': review,
'rating': rating,
'timeSlotId': timeSlot.timeSlotId,
'userId': UserService().currentUserFirebase!.uid,
'doctorId': timeSlot.doctorid,
'user': {
'displayName': UserService().currentUserFirebase!.displayName,
'photoUrl': UserService().getProfilePicture(),
}
});
} catch (e) {
return Future.error(e.toString());
}
}
Future<List<ReviewModel>> getDoctorReview(
{required Doctor doctor, int limit = 4}) async {
try {
var reviewRef = await FirebaseFirestore.instance
.collection('Review')
.where('doctorId', isEqualTo: doctor.doctorId)
.limit(limit)
.get();
List<ReviewModel> listReview = reviewRef.docs.map((doc) {
var data = doc.data();
data['reviewId'] = doc.reference.id;
ReviewModel review = ReviewModel.fromMap(data);
return review;
}).toList();
return listReview;
} catch (e) {
return Future.error(e.toString());
}
}
}


You are not updating the "rating" variable in the controller