Remove Index wise CustomWidget from List i

2019-07-20 01:47发布

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;
          });

1条回答
Deceive 欺骗
2楼-- · 2019-07-20 02:33

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.

查看更多
登录 后发表回答