我如何根据月份对列表进行分类
假设我有如下示例列表:
Record(
id: '01',
nameFood: 'ravioli',
date : 2021/may/14,
price: 20
),
Record(
id: '02',
nameFood: 'cheese garlic',
date : 2021/june/02,
price: 30,
),
Record(
id: '03',
nameFood: 'steak',
date : 2021/march/21
price 25,
),
我想把它分类为喜欢这个
June(
nameFood: steak,
total price: 240,
),
and etc
问题是我不知道如何过滤每个列表(月),因为每个月都有不同的天数,如 30,31 和 28
如果您将日期存储为 DateTime,则可以尝试使用月份数字。假设您有一个 Record 类型的对象列表,Record 就在这里。
class Record {
Record(
this.id,
this.price,
this.date,
this.nameFood,
);
final String id;
final double price;
final DateTime date;
final String nameFood;
}
您可以循环浏览您的列表,然后做任何您想做的事情,就像这样。
for (final record in _listOfRecords) {
final _monthOfRecord = record.month; // Gives the month number. January = 1, April = 4, etc.
// Do other stuff, based on the current monthNumber
switch (_monthOfRecord) {
case 1:
// Do something
break;
....
default:
break;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句