flutter - setState 没有第一时间更新 UI

问题描述 投票:0回答:1

单击对话框的提交按钮时会触发我的应用程序中的 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,
                      ),
                    ),
                  ],
                ),
              );
            }));
  }

仅当我单击两次提交按钮时,UI 才会更新,同时正常调用和评估 SubmitReview 和 editReview 函数。

flutter dart
1个回答
0
投票

我认为你应该在调用 setState 之前等待调用函数的位置,你可以尝试下面的代码。

 TextButton(
              child: Text("SUBMIT"),
              onPressed: () async {
                if (comment != null || rating != null) {
                 
                   await editReview();
                 setState(() {
                  });
                } else {
                  
                   await submitReview();
                   setState(() {
                  });
                }
              },
            ),

我希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.