【问题标题】:How to remove specific items from a list?如何从列表中删除特定项目?
【发布时间】:2019-03-17 15:07:53
【问题描述】:

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

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.."
    )
);

【问题讨论】:

    标签: flutter


    【解决方案1】:

    removeWhere 允许这样做:

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

    另见List Dartdoc

    【讨论】:

    【解决方案2】:

    在你的情况下这是可行的:

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

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

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

    【讨论】:

    • listname.remove(item);对我来说很好用!!
    【解决方案3】:
    //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)
    

    【讨论】:

      【解决方案4】:

      这也有效

      _listofTaskUI.removeAt(_quantity);
      

      【讨论】:

        【解决方案5】:

        如果你有一个通用列表

        List<int> sliderBtnIndex = [];//Your list
        
        sliderBtnIndex.remove(int.tryParse(index)); //remove
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-11-17
          • 2022-01-08
          • 2018-05-03
          • 1970-01-01
          • 2012-03-22
          • 2016-01-29
          相关资源
          最近更新 更多