dart 如何从列表中删除特定项目?

yqkkidmi  于 2023-03-27  发布在  其他
关注(0)|答案(6)|浏览(120)

如何使用id = 001删除List<ReplyTile>上的项目..?

final List<ReplyTile> _replytile = <ReplyTile>[];

_replytile.add(
    ReplyTile(
        member_photo: 'photo',
        member_id: '001',
        date: '01-01-2018',
        member_name: 'Denis',
        id: '001',
        text: 'hallo..'
    )
);
rbpvctlc

rbpvctlc1#

removeWhere允许这样做:

replytile.removeWhere((item) => item.id == '001')

另请参见List Dartdoc

i1icjdpr

i1icjdpr2#

在您的情况下,这起作用:

replytile.removeWhere((item) => item.id == '001');

对于具有特定数据类型(如int)的列表,remove也可以工作。例如:

List id = [1,2,3];
id.remove(1);
6xfqseft

6xfqseft3#

//For removing specific item from a list with the attribute value
replytile.removeWhere((item) => item.id == '001') 

//Remove item by specifying the position of the item in the list
replytile.removeAt(2)   

//Remove last item from the list
replytile.removeLast()

//Remove a range of items from the list
replytile.removeRange(2,5)
4c8rllxm

4c8rllxm4#

这个也行

_listofTaskUI.removeAt(_quantity);
r55awzrz

r55awzrz5#

如果您有一个通用列表

List<int> sliderBtnIndex = [];//Your list

sliderBtnIndex.remove(int.tryParse(index)); //remove
qzlgjiam

qzlgjiam6#

这里是简单的列表函数和示例输出

考虑样本列表

List sampleList = ["001", "002", "003"];

这里有一些带输出的add to list函数

//Add to list

  sampleList.add("004");
//Output: 001 002 003 004

//Add to specific index

  sampleList.insert(0, "005");
//Output: 005 001 002 003

这里的列表移除函数与示例考虑的是相同的示例列表

sampleList.removeWhere((item) => item.id == '003');
//Output: 001 002

//Remove item from secific index

  sampleList.removeAt(2);
//Output: 001 003

//Remove item from last

  sampleList.removeLast();
//Output: 001 002

//Remove items between range

  sampleList.removeRange(1, 2);
//Output: 002 003

相关问题