我的应用程序中的setState方法是在单击对话框的提交按钮时触发的
TextButton(
child: Text("SUBMIT"),
onPressed: () async {
if (comment != null || rating != null) {
setState(() {
editReview();
});
} else {
setState(() {
submitReview();
});
}
},
),
编辑审阅和提交审阅函数的调用为:
submitReview() async {
final user = Provider.of<UsersProvider>(context, listen: false).user;
final restaurant =
Provider.of<RestaurantProvider>(context, listen: false).restaurant1;
final comment = commentText.text;
final rating = ratingValue;
bool response =
await Provider.of<RestaurantReviewProvider>(context, listen: false)
.addReview(restaurant.id!, user.id!, rating, comment);
if (response) {
dx.showSuccessMsg(context, "Completed", "Thanks for your feedback");
} else {
dx.errorAlert(context, "Already submit the feedback.");
}
}
editReview() async {
final review =
Provider.of<RestaurantReviewProvider>(context, listen: false).review;
final user = Provider.of<UsersProvider>(context, listen: false).user;
final restaurant =
Provider.of<RestaurantProvider>(context, listen: false).restaurant1;
final comment = commentText.text;
final rating = ratingValue;
bool response = await Provider.of<RestaurantReviewProvider>(context,
listen: false)
.updateReview(review.id!, restaurant.id!, user.id!, rating, comment);
if (response) {
dx.showSuccessMsg(
context, "Update Successfull", "Your feedback has been updated!");
} else {
dx.errorAlert(context, "Invalid");
}
}
这是我想要显示数据的UI:
Widget showComment(BuildContext context, List<dynamic> data) {
return SizedBox(
child: ListView.builder(
itemCount: data.length,
shrinkWrap: true,
itemBuilder: (BuildContext context, int index) {
return Container(
margin: EdgeInsets.only(top: 10.h),
padding: EdgeInsets.all(10.h),
width: MediaQuery.of(context).size.width,
height: 80.h,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(10),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Text(
"${data[index].userFullName}",
style: TextStyle(
fontSize: 12.h,
fontWeight: FontWeight.bold,
),
),
Spacer(),
RatingBar(
ignoreGestures: true,
initialRating: data[index].rating,
allowHalfRating: false,
direction: Axis.horizontal,
itemCount: 5,
itemSize: 15,
ratingWidget: RatingWidget(
full:
Icon(Icons.star_rounded, color: Colors.yellow),
half: Icon(Icons.star_half_rounded,
color: Colors.yellow),
empty: Icon(Icons.star_border_rounded,
color: Colors.black),
),
onRatingUpdate: (rating) {},
),
],
),
SizedBox(height: 4.h),
Text(
DateFormat('dd/MM/yyyy').format(data[index].createdDate),
style: TextStyle(fontSize: 8.h, color: Colors.grey),
),
SizedBox(height: 10.h),
Text(
"${data[index].comment}",
style: TextStyle(
fontSize: 10.h,
),
),
],
),
);
}));
}
只有当我单击submit按钮两次时,UI才会更新,而submitReview和editReview函数会被正常调用和计算。
1条答案
按热度按时间p4rjhz4m1#
我认为你应该在调用setState之前等待你调用函数的位置,你可以试试下面的代码。
希望这个能帮上忙。