Search code examples
flutterlistviewflutter-getx

Getx , Flutter how to update state of a variable in a listview builder everytime list loads its taking initial index value only?


so i have to create a wishlist , and i am getting data from api . If the api returns outofstock = true , i will need to hide add to cart button and if the api returns in stock i will need to display the button . The problem is in getx we need obs variable to change state , and if i create an obs variable and update its value everytime its still taking the initial value.

i tried creating an obs variable to collect response from api and save it to obs variable then use that variable to check if its in stock then show the button else not , but the value of obs variable is false everytime help ListView.builder( physics: const NeverScrollableScrollPhysics(), shrinkWrap: true, itemCount: controller.wishlistData.length, itemBuilder: (BuildContext context, int index) { final data =controller.wishlistData[index]; controller.outOfStockFlag.value = data.variantMeta!.bOutOfStockFlag!; the value of controller.outOfStockFlag is intially set to true then for next build it remains staticall true, where as in response i am getting true as well as false


Solution

  • When you get data from api, you need check to change the obs variable. Eg:

    void _checkData() async {
    var data = await ...;
    if(...) outofstock.value = true;
    else outofstock.value = false;
    update();
    }