Remove Index wise CustomWidget from List<Widget> in Flutter

Issue

I have initially empty list of Widget in Column. Now on Other widget click I am adding new Custom Widget in _contactItems

   Column(
      children: _contactItems,
    )

 List<Widget> _contactItems = new List<CustomWidget>();



 _contactItems.add(newCustomWidget(value));

Now Suppose I have 6 Records (6 Custom Widgets in Column). I am trying to remove index wise records (Example. I am removing 3rd record then 1st record. Column Widgets (dynamic widgets) should be updated as _contactItems updating in setState())

Now on CustomWidget click I am removing that particular CustomWidget from Column.

setState(() {
          _contactItems.removeAt(index);
        });

Also tried with

_contactItems.removeWhere((item) {
            return item.key == _contactItems[index].key;
          });

Solution

Try this (assuming that your Column widget keys have this format):

setState(() {
  this._contactItems.removeWhere((contact) => contact.key == Key("index_$index"));
});

If this doesn’t solve your issue, maybe we’ll need more info.

Answered By – Zroq

Answer Checked By – Terry (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.