Flutter setstate on local variable not updating UI

Viewed 747

So I tried to dynamically update an individual listview item. I am using a function to generate each item and I created a local variable in that function. I used setstate to update that variable but the UI is not updating. If i make the same variable global it works but i changes every listview items which I dont want. Does anyone have any tips on doing that? I am new to flutter and Sorry if this has been asked multiple times. I tried searching but couldnt find any solutions. This is a similar code for the item builder function.

Widget generateListItem(){ 
    bool flag= false;
    return InkWell(
       onTap:(){
          setState((){
             flag=true;
          })
       },
       child: Text(flag? "Changed" : "Not Changed"),
    );
}
1 Answers

the flag is resetted every time you call setState, beacuse the flag is in your generateListItem() widget,

I think you must set the bool in a list, so when you called setState, the state in the list of bool not resetted,

List<bool> flag = [];  // 

Widget generateListItem(int i){ 
flag.add(false);
return InkWell(
   onTap:(){
      setState((){
         flag[i]=true;
      })
   },
   child: Text(flag[i]? "Changed" : "Not Changed"),
);}
Related