gpt4 book ai didi

list - ListView 使用Flutter中的属性过滤掉最后一行数据

转载 作者:行者123 更新时间:2023-12-03 03:24:13 24 4
gpt4 key购买 nike

我有一个列表,其中包含项目名称,索引和计数。现在有不止一个项目具有相同的索引,但是我想将它们过滤到一个新的List中,该列表仅包含每个索引的最后一行数据。

例如,如果我的列表如下:

ALPHa  index 0 and count is 2
ALPHa index 0 and count is 3
ALPHa index 0 and count is 4
ALPHa index 0 and count is 5
ALPHa index 0 and count is 4
ALPHa index 0 and count is 3
ALPHa index 0 and count is 2
ALPHa index 0 and count is 1
ALPHa index 0 and count is 5
ALPHa index 0 and count is 0
BETA index 1 and count is 1
BETA index 1 and count is 2
BETA index 1 and count is 3
BETA index 1 and count is 4
BETA index 1 and count is 3
BETA index 1 and count is 2
BETA index 1 and count is 3
BETA index 1 and count is 4
DELTA index 2 and count is 4
DELTA index 2 and count is 1
DELTA index 2 and count is 2
DELTA index 2 and count is 3

过滤后的列表应该像
ALPHa  index 0 and count is 0
BETA index 1 and count is 4
DELTA index 2 and count is 3

有人可以告诉我如何将其过滤掉吗?

我的代码如下:

模态课:
class CartItems {
String itemname;
int numberofitems;
int index;

CartItems({
this.itemname,
this.numberofitems,
this.index
});
}

list
List <CartItems> cartItems = List();
for(int i =0; i < itemList.length; i++) {
cartItems.add(CartItems(itemname: itemList[i].itemname, numberofitems: itemList[i].numberofitems,index:index));
}

最佳答案

试试这个代码
将您的原始列表作为参数传递给该方法,该方法将返回过滤的 CartItems 列表

List<CartItems> filterList(List<CartItems> itemList){
int currentItemsIndex = 0;
CartItems cartItem;
List < CartItems > cartItems = List();
for(int i =0 ;i< itemList.length; i++) {
cartItem = CartItems(itemname: itemList[i].itemname, numberofitems: itemList[i].numberofitems,index:itemList[i].index);
if(currentItemsIndex == itemList[i].index){
if(i == itemList.length-1){
cartItems.add(cartItem);
}
}else{
cartItems.add(cartItem);
}
currentItemsIndex = itemList[i].index;

}
return cartItems;
}

关于list - ListView 使用Flutter中的属性过滤掉最后一行数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59466113/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com