2

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"),
    );
}
7
  • 1
    When rebuild 'generateListItem' by setState(), flag is also reset. So always flag is false. Commented Oct 20, 2020 at 2:37
  • Ohh.. That makes more sense now. Is there any way to update the item individually? Commented Oct 20, 2020 at 2:57
  • 1
    Make it a variable inside the State class instead of inside the method. @Bikramgurung Commented Oct 20, 2020 at 2:58
  • If I do that setstate changes every Item. I want to individually change the specific item. Commented Oct 20, 2020 at 2:59
  • You need to control that value at top parent widget re-builded. "Page(here)[Stateful widget] - build() - 'generateListItem' widget..." Commented Oct 20, 2020 at 2:59

1 Answer 1

1

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"),
);}
Sign up to request clarification or add additional context in comments.

1 Comment

amazing! Map<String,bool> could be too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.