我有一个变量,我给它赋值,值在gridView.Builder以内,有一个按钮,当单击我的变量应该更改时,我使用setState进行更改,但它没有更改,这可能是什么原因?

class _CatalogItemsState extends State<CatalogItems> {
  Set<int> _isFavLoading = {};
  bool isFavorite = false;
@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(name!),
      ),
      body: Padding(
        padding: const EdgeInsets.only(left: 10, right: 10),
        child: Column(
          children: [
            Expanded(
              child: FutureBuilder<List<Product>>(
                  future: productFuture,
                  builder: (context, snapshot) {
                    if (snapshot.connectionState == ConnectionState.waiting) {
                      return buildGridShimmer();
                    } else if (snapshot.hasData) {
                      final catalog = snapshot.data;
                      if (catalog!.isEmpty) {
                        return const Center(
                          child: Text(
                            'Нет товаров',
                            style: TextStyle(
                                fontSize: 25, fontWeight: FontWeight.bold),
                          ),
                        );
                      }
                      return buildCatalog(catalog);
                    } else {
                      print(snapshot.error);
                      return const Text("No widget to build");
                    }
                  }),
            ),
          ],
        ),
      ),
    );
  }


SliverGrid(
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(),
            delegate: SliverChildBuilderDelegate(childCount: product.length,
                (BuildContext context, int index) {
              final media =
                  product[index].media?.map((e) => e.toJson()).toList();
              final photo = media?[0]['links']['local']['thumbnails']['350'];
              final productItem = product[index];
              isFavorite = productItem.is_favorite; // this is the new value of the variable, work good
 
IconButton(
                                    icon: Icon(_isFavLoading.contains(index) || isFavorite ? Icons.favorite : Icons.favorite_border, color: Colors.red,),
                                    onPressed: () {
                                       setState(() {
                                          isFavorite = !isFavorite;
                                        });
                                        print('t: ${isFavorite}'); // the value of the variable does not change
                                    },
                              )

推荐答案

这是因为您并没有真正更新您的product对象.当按下图标时,您必须更改其值

onPressed: () {
    setState(() {
       productItem.is_favorite = !isFavorite;
    });
    print('t: ${productItem.is_favorite}'); // the value of the variable does not change
  }

Flutter相关问答推荐

无法让Riverpod Stream发出新值

如何知道当前屏幕是否处于抖动状态?

在框中保存对象列表时,类型推断无法正常工作

使用Future函数的容器中未显示图像,但在使用图像小工具时显示

UI中的Flutter 下拉列表值未更改

将自定义容器附加到屏幕底部

如何从flutter中不同类别的列表中获取所有产品

Flutter - Stripe:类型List 不是类型Map 的子类型? method_channel_stripe.dart 中的错误:263

仅在获取(读取)、Provider 时,Firestore 中的数据为空

点击按钮后重新启动定时器

如何使用 riverpod_annotation 创建非自动处置提供程序?

当我将主题更改为深色然后返回浅色模式时,Getx 会Flutter 更改主题,它不会加载我的自定义主题

Flutter Bloc 使用 Cubit 监听流和emits 状态

如何使用 Rrect 或类似工具在 flutter 中绘制梯形线?

改变步进器的线条 colored颜色 Flutter

Flutter HLS .m3u8 视频无法实时播放

如何获取 android 和 Ios (flutter) 的 CircularProgress 指示器?

在 Flutter 中,您什么时候应该更喜欢Widget继承而不是组合?

一个或多个插件需要更高的 Android SDK 版本

Android Studio 模拟器屏幕闪烁