作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有以下列表:
List<MoveItem> _items = [
MoveItem(
shelf: A01,
itemName: 'Dumpling',
quantity: 3,
expiryDate: DateTime(2021, 12, 8),
),
MoveItem(
shelf: A02,
itemName: 'Dumpling',
quantity: 3,
expiryDate: DateTime(2021, 12, 8),
),
MoveItem(
shelf: A03
itemName: 'Dumpling',
quantity: 3,
expiryDate: DateTime(2022, 01, 8),
),
MoveItem(
shelf: A04,
itemName: 'Meatball',
quantity: 3,
expiryDate: DateTime(2022, 02, 8),
),
MoveItem(
shelf: A05,
itemName: 'Meatball',
quantity: 3,
expiryDate: DateTime(2022, 02, 8),
),];
如何按itemName和expiryDate对列表进行分组?
ListView.builder(
itemCount: _items.length,
itemBuilder: (ctx, i) =>
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
_items[i].itemName,
),
Text(DateFormat('yyyy-MM-dd')
.format(_items[i].expiryDate)),
Text(
_items[i].quantity.toString(),
)]),
我会得到这个:
Dumpling 2021-12-8 3
Dumpling 2021-12-8 3
Dumpling 2022-01-8 3
Meatball 2022-02-8 3
Meatball 2022-02-8 3
我需要的结果:
Dumpling 2021-12-8 6
Dumpling 2022-01-8 3
Meatball 2022-02-8 6
我还需要将每个组的数据传递到小部件树下。
最佳答案
欢迎使用堆栈溢出。
您可以使用商品名称和日期对它们进行分组,因为您希望这些值相同:
Map<String, MoveItem> groups = {};
for (MoveItem item in _items) {
String convertedDateTime =
"${item.expiryDate.year.toString()}-${item.expiryDate.month.toString().padLeft(2, '0')}-${item.expiryDate.day.toString().padLeft(2, '0')}";
if (groups[item.itemName + ' ' + convertedDateTime] == null)
groups[item.itemName + ' ' + convertedDateTime] = MoveItem(
expiryDate: item.expiryDate,
quantity: item.quantity,
itemName: item.itemName,
);
else
groups[item.itemName + ' ' + convertedDateTime].quantity += item.quantity;
}
输出:
{
Dumpling 2021-12-08:Dumpling - 2021-12-08 6,
Dumpling 2022-01-08:Dumpling - 2022-01-08 3,
Meatball 2022-02-08:Meatball - 2022-02-08 6
}
我的模特:
class MoveItem {
String itemName;
int quantity;
DateTime expiryDate;
MoveItem({this.itemName, this.quantity, this.expiryDate});
@override
String toString() {
String convertedDateTime =
"${expiryDate.year.toString()}-${expiryDate.month.toString().padLeft(2, '0')}-${expiryDate.day.toString().padLeft(2, '0')}";
return itemName + ' - ' + convertedDateTime + ' ' + quantity.toString();
}
}
编辑:
List<MoveItem> groups = [];
for (int i = 0; i < _items.length; i++) {
bool groupExists = false;
for (MoveItem group in groups) {
if (group.itemName == _items[i].itemName &&
group.expiryDate == _items[i].expiryDate) {
group.quantity += _items[i].quantity;
groupExists = true;
}
}
if (!groupExists) {
groups.add(_items[i]);
}
}
输出:
[Dumpling - 2021-12-08 6, Dumpling - 2022-01-08 3, Meatball - 2022-02-08 6]
在ListView中使用它:
ListView.builder(
itemCount: groups.length,
itemBuilder: (ctx, i) => Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
groups[i].itemName,
),
Text(DateFormat('yyyy-MM-dd')
.format(groups[i].expiryDate)),
Text(
groups[i].quantity.toString(),
)
],
),
),
关于flutter - 如何在Dart中按多个值对列表进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63562148/
我是一名优秀的程序员,十分优秀!