Shailendra Pundhir
Shailendra Pundhir

Reputation: 11

Flutter ListView.builder on remove element not updating list

I have a ListView.builder widget

void delete(dynamic obj) {
  setState((){
    pc.remove(obj);
  })
}

void update(dynamic obj){}

List pc = List();

@override
void init(){
  super.initState();
  //Fetch data
  setState((){
    pc = data;
  })
}

@override
Widget build(BuildContext context) {
  return Container(
    child: ListView.builder(
      padding: EdgeInsets.only(bottom: 72.0),
      itemCount: pc.length,
      itemBuilder: (context, index) {
        return ListItemWid(
          pc.elementAt(index), delete, update);
      },
    ),
  );
}

Here is have shown dynamic as the element type but actually is a fixed object. now when the ListItemWid's delete button is called I call the delete function in the stateful widget, and the code in the function is executed, because the object is removed from db. But it is not reflected properly in the View. Sometimes, wrong element is removed, sometimes no element is removed at all. Sometimes it works. I cannot post the actual source code because of restrictions, but I really need help with modifying elements in list view in flutter. The standard methods as demonstrated in flutter gallery are not working.

Just for more information, the list view is shown inside one of the PageView pages and all data is fetched and shown from Sqflite in the current list widget only.

Please help.

Thanks.

Upvotes: 1

Views: 6192

Answers (2)

Manish sahu
Manish sahu

Reputation: 333

if you delete particular index value use this

listName.removeAt(index);

Upvotes: 0

abdalmonem
abdalmonem

Reputation: 1422

you should add a key to the returned widget of your ListView builder i recommend you to see this video from google devolopers channel : https://www.youtube.com/watch?v=kn0EOS-ZiIc it well help you to understand more about "keys"

Upvotes: 7

Related Questions