Remove Index wise CustomWidget from List<Widget> in Flutter

寵の児 提交于 2019-12-11 01:15:27

问题


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:


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.



来源:https://stackoverflow.com/questions/54310614/remove-index-wise-customwidget-from-listwidget-in-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!