Flutter:从另一个 DateTime 列表生成每个月的总销售额列表
Flutter : Generate List of total sales for each month from another list of DateTime
我有一份每日销售清单,其中包括每个条目的“DateTime”和“Int”。
final data = [
DailySales(2022-03-10 00:00:00.000, 200),
DailySales(2022-03-09 00:00:00.000, 255),
DailySales(2022-02-08 00:00:00.000, 100),
DailySales(2022-02-10 00:00:00.000, 520),
DailySales(2022-01-08 00:00:00.000, 100),
DailySales(2022-01-10 00:00:00.000, 520),
];
class DailySales {
final DateTime day;
final int sales;
DailySales(this.day, this.sales);
}
现在根据上面的列表,我需要动态生成一个新列表,其中应包含每个月份的名称和当月的总销售额。
如下模型所示:
class MonthlySales {
final String month;
final int sales;
MonthlySales(this.month, this.sales);
}
请帮忙解决这个问题
你可以这样做:
main() {
final data = [
DailySales(DateTime.parse("2022-03-10 00:00:00.000"), 200),
DailySales(DateTime.parse("2022-03-09 00:00:00.000"), 255),
DailySales(DateTime.parse("2022-02-08 00:00:00.000"), 100),
DailySales(DateTime.parse("2022-02-10 00:00:00.000"), 520),
DailySales(DateTime.parse("2022-01-08 00:00:00.000"), 100),
DailySales(DateTime.parse("2022-01-10 00:00:00.000"), 520),
];
Map tempMap = Map();
for (DailySales item in data) {
if (tempMap[item.day.month] == null) {
tempMap[item.day.month] = item.sales;
} else {
tempMap[item.day.month] = tempMap[item.day.month] + item.sales;
}
}
List monthlySales = [];
for (int i = 1; i <= 12; i++) {
if (tempMap[i] != null)
monthlySales.add(MonthlySales(i.toString(), tempMap[i]));
}
print(monthlySales);
}
class DailySales {
final DateTime day;
final int sales;
DailySales(this.day, this.sales);
}
class MonthlySales {
final String month;
final int sales;
MonthlySales(this.month, this.sales);
@override
String toString() {
// TODO: implement toString
return "month " + month + " sales " + sales.toString();
}
}
输出看起来像:
[month 1 sales 620, month 2 sales 620, month 3 sales 455]
这是您需要做的:-
void main() {
final data = [
DailySales(day: DateTime(2022, 03, 09), sales: 200),
DailySales(day: DateTime(2022, 03, 08), sales: 255),
DailySales(day: DateTime(2022, 03, 07), sales: 100),
DailySales(day: DateTime(2022, 03, 04), sales: 520),
DailySales(day: DateTime(2022, 03, 02), sales: 100),
DailySales(day: DateTime(2022, 03, 03), sales: 520),
];
List<MonthlySales> monthlySales = data
.map((dailySale) => MonthlySales(
month: dailySale.day.month.toString(), sales: dailySale.sales))
.toList();
print(monthlySales.runtimeType);
print(monthlySales.toString());
}
class DailySales {
final DateTime day;
final int sales;
DailySales({required this.day, required this.sales});
}
class MonthlySales {
final String month;
final int sales;
MonthlySales({required this.month, required this.sales});
}
输出如下:-
List<MonthlySales>
[Instance of 'MonthlySales', Instance of 'MonthlySales', Instance of 'MonthlySales', Instance of 'MonthlySales', Instance of 'MonthlySales', Instance of 'MonthlySales']
Dart 集合提供了很多这样丰富的方法,比如上面的 map 方法来简化业务逻辑。
一次访问 it 以获得更多帮助。
你可以使用这个包来分组数据collection
collection: ^1.15.0
List<MonthlySales> OrderSalebymonth() {
List<String> months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
final datas = [DailySales(DateTime.parse("2022-03-10 00:00:00.000"), 200), DailySales(DateTime.parse("2022-03-09 00:00:00.000"), 255), DailySales(DateTime.parse("2022-02-08 00:00:00.000"), 100), DailySales(DateTime.parse("2022-02-10 00:00:00.000"), 520), DailySales(DateTime.parse("2022-01-08 00:00:00.000"), 100), DailySales(DateTime.parse("2022-01-10 00:00:00.000"), 520),];
List<MonthlySales> monthlist = [];
var groupBy2 = groupBy(datas, (DailySales sale) => sale.day.month);
groupBy2.forEach((key, value) {print("=================Month:$key ${months[key - 1]}=============");value.forEach((element) {monthlist.add(MonthlySales(months[key - 1], element.sales));print(element.sales.toString());});});
return monthlist;
}
class DailySales {
final DateTime day;
final int sales;
DailySales(this.day, this.sales);
}
class MonthlySales {
final String month;
final int sales;
MonthlySales(this.month, this.sales);
}
预期输出
=================Month:3 March=============
200
255
=================Month:2 February=============
100
520
=================Month:1 January=============
100
520
我有一份每日销售清单,其中包括每个条目的“DateTime”和“Int”。
final data = [
DailySales(2022-03-10 00:00:00.000, 200),
DailySales(2022-03-09 00:00:00.000, 255),
DailySales(2022-02-08 00:00:00.000, 100),
DailySales(2022-02-10 00:00:00.000, 520),
DailySales(2022-01-08 00:00:00.000, 100),
DailySales(2022-01-10 00:00:00.000, 520),
];
class DailySales {
final DateTime day;
final int sales;
DailySales(this.day, this.sales);
}
现在根据上面的列表,我需要动态生成一个新列表,其中应包含每个月份的名称和当月的总销售额。
如下模型所示:
class MonthlySales {
final String month;
final int sales;
MonthlySales(this.month, this.sales);
}
请帮忙解决这个问题
你可以这样做:
main() {
final data = [
DailySales(DateTime.parse("2022-03-10 00:00:00.000"), 200),
DailySales(DateTime.parse("2022-03-09 00:00:00.000"), 255),
DailySales(DateTime.parse("2022-02-08 00:00:00.000"), 100),
DailySales(DateTime.parse("2022-02-10 00:00:00.000"), 520),
DailySales(DateTime.parse("2022-01-08 00:00:00.000"), 100),
DailySales(DateTime.parse("2022-01-10 00:00:00.000"), 520),
];
Map tempMap = Map();
for (DailySales item in data) {
if (tempMap[item.day.month] == null) {
tempMap[item.day.month] = item.sales;
} else {
tempMap[item.day.month] = tempMap[item.day.month] + item.sales;
}
}
List monthlySales = [];
for (int i = 1; i <= 12; i++) {
if (tempMap[i] != null)
monthlySales.add(MonthlySales(i.toString(), tempMap[i]));
}
print(monthlySales);
}
class DailySales {
final DateTime day;
final int sales;
DailySales(this.day, this.sales);
}
class MonthlySales {
final String month;
final int sales;
MonthlySales(this.month, this.sales);
@override
String toString() {
// TODO: implement toString
return "month " + month + " sales " + sales.toString();
}
}
输出看起来像:
[month 1 sales 620, month 2 sales 620, month 3 sales 455]
这是您需要做的:-
void main() {
final data = [
DailySales(day: DateTime(2022, 03, 09), sales: 200),
DailySales(day: DateTime(2022, 03, 08), sales: 255),
DailySales(day: DateTime(2022, 03, 07), sales: 100),
DailySales(day: DateTime(2022, 03, 04), sales: 520),
DailySales(day: DateTime(2022, 03, 02), sales: 100),
DailySales(day: DateTime(2022, 03, 03), sales: 520),
];
List<MonthlySales> monthlySales = data
.map((dailySale) => MonthlySales(
month: dailySale.day.month.toString(), sales: dailySale.sales))
.toList();
print(monthlySales.runtimeType);
print(monthlySales.toString());
}
class DailySales {
final DateTime day;
final int sales;
DailySales({required this.day, required this.sales});
}
class MonthlySales {
final String month;
final int sales;
MonthlySales({required this.month, required this.sales});
}
输出如下:-
List<MonthlySales>
[Instance of 'MonthlySales', Instance of 'MonthlySales', Instance of 'MonthlySales', Instance of 'MonthlySales', Instance of 'MonthlySales', Instance of 'MonthlySales']
Dart 集合提供了很多这样丰富的方法,比如上面的 map 方法来简化业务逻辑。
一次访问 it 以获得更多帮助。
你可以使用这个包来分组数据collection
collection: ^1.15.0
List<MonthlySales> OrderSalebymonth() {
List<String> months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
final datas = [DailySales(DateTime.parse("2022-03-10 00:00:00.000"), 200), DailySales(DateTime.parse("2022-03-09 00:00:00.000"), 255), DailySales(DateTime.parse("2022-02-08 00:00:00.000"), 100), DailySales(DateTime.parse("2022-02-10 00:00:00.000"), 520), DailySales(DateTime.parse("2022-01-08 00:00:00.000"), 100), DailySales(DateTime.parse("2022-01-10 00:00:00.000"), 520),];
List<MonthlySales> monthlist = [];
var groupBy2 = groupBy(datas, (DailySales sale) => sale.day.month);
groupBy2.forEach((key, value) {print("=================Month:$key ${months[key - 1]}=============");value.forEach((element) {monthlist.add(MonthlySales(months[key - 1], element.sales));print(element.sales.toString());});});
return monthlist;
}
class DailySales {
final DateTime day;
final int sales;
DailySales(this.day, this.sales);
}
class MonthlySales {
final String month;
final int sales;
MonthlySales(this.month, this.sales);
}
预期输出
=================Month:3 March=============
200
255
=================Month:2 February=============
100
520
=================Month:1 January=============
100
520