FutureBuilder 在我调用 setState 时不断重建

问题描述 投票:0回答:0
Positioned(
                                          top: 12,
                                          right: 12,
                                          child: SizedBox(
                                            height: 25,
                                            width: 25,
                                            child: CircleAvatar(
                                              backgroundColor: isGolden? Colors.amber :Colors.black87,
                                              child: IconButton(
                                                padding: EdgeInsets.zero,
                                                icon: Icon(Icons.star,),
                                                color: Color(0xFFFFFFD5),
                                                onPressed: (){
                                                  futureMessage = PostStoryFavoriteCreateDelete(snapshot.data!.data![index].id.toString());
                                                  ScaffoldMessenger.of(context).showSnackBar(SnackBar(elevation: 20,content: FutureBuilder(
                                                    future: futureMessage,
                                                    builder: (context, snapshot){
                                                      if (snapshot.hasData) {
                                                        return Text("SuccessFully Updated");
                                                      } else if (snapshot.hasError) {
                                                        return Text("Failed To Update");
                                                      }
                                                      return const Text("Processing...");
                                                    },
                                                  ),));
                                                  setState(() {
                                                    isGolden == true ?isGolden = false:isGolden = true;
                                                  });
                                                },
                                              ),
                                            ),
                                          ),
                                        )
itemBuilder: (BuildContext context, index){
                            isGolden = snapshot.data!.data![index].favoriteStories!.length>0 ? true :false;

onPressed() 不更新我的状态,即改变我的星星的颜色,我必须再次获取 api 才能更新我的星星的颜色。或者我必须刷新我的页面。

flutter state future builder
© www.soinside.com 2019 - 2024. All rights reserved.