not fixed itemCount in a Listview.builder

ForumCategory: Questionsnot fixed itemCount in a Listview.builder
lucastsc asked 9 months ago

I have a Listview.builder where, on each item’s onLongPress, I should change the itemCount, but I don’t know how to do this.In other words, I need to have an itemCount with variable value by some way, how can I accomplish this?

The way the code was shown, at the itemBuilder the “index” varies from 0 to “contactsList.length”.However, I’ve set an onLongPress where I change
“contactsList.length”.So, later in the code, when I use “index”, itemCount thinks that index varies from 0 to “contactsList.length”, but the later was updated.

child: ListView.builder(
  itemCount: contactsList.length,
  itemBuilder: (context, index) {
    return GestureDetector(
      onTap: (){
        print("apertou $index");
        print("contactsList.length: ${contactsList.length}");
      },
      onLongPress: (){
        setState(() {
          contactsList.removeAt(index);
          //helper.deleteContact('name', contactsList[index].name);
        });
      },