Skip to content

ListView.builder widget remove element not reflected sometimes #29533

@shailendrapundhir

Description

@shailendrapundhir

I have a ListView.builder widget

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

void update(dynamic obj){}

List<dynamic> 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 used dynamic as the element type , actual source code has a fixed object type.
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions