Flutter删除列表项

Den*_*dan 5 dart flutter

我的flartter dart脚本中有一个问题,这是我的脚本:

List<ReplyTile> replytile = new List<ReplyTile>();

replytile.add(
    new ReplyTile(
        member_photo: "photo",
        member_id: "001",
        date: "01-01-2018",
        member_name: "Denis",
        id: "001",
        text: "hallo.."
    )
);
Run Code Online (Sandbox Code Playgroud)

我的问题是:如何删除上的项目List<ReplyTile>id = 001..?

提前致谢

Gün*_*uer 27

removeWhere 允许这样做:

replytile.removeWhere((item) => item.id == '001')
Run Code Online (Sandbox Code Playgroud)

另请参见List Dartdoc

  • 如果 item.id == '001' 是异步的怎么办? (2认同)

Cod*_*ker 23

//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)
Run Code Online (Sandbox Code Playgroud)


Suz*_*tha 14

在您的情况下,这有效:

replytile.removeWhere((item) => item.id == '001');
Run Code Online (Sandbox Code Playgroud)

对于具有特定数据类型(例如 int)的列表,删除也有效。例如:

List id = [1,2,3];
id.remove(1);
Run Code Online (Sandbox Code Playgroud)

  • 列表名称. 删除(项目); 对我来说效果很好! (2认同)

Sil*_*der 5

这也有效

_listofTaskUI.removeAt(_quantity);
Run Code Online (Sandbox Code Playgroud)