Home > Mobile >  why setState does not change my variable Flutter?
why setState does not change my variable Flutter?

Time:12-20

I have a variable to which i assign a value inside gridView.Builder and there is a button, when clicked on which my variable should change, I use setState for this, but it does not change, what could be the reason?

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
                                    },
                              )

CodePudding user response:

This is because you're not really updating your product object. You must change its value when icon is pressed

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