嘿,我有这个来自api的示例列表数据,我尝试将其显示到fl_charts
[
{"type": "expense", "amount": 250, "paymentDate": "22-02-2022"},
{"type": "income", "amount": 350, "paymentDate": "22-02-2022"},
{"type": "expense", "amount": 150, "paymentDate": "22-02-2022"},
{"type": "expense", "amount": 450, "paymentDate": "22-03-2022"},
{"type": "income", "amount": 650, "paymentDate": "22-03-2022"},
{"type": "expense", "amount": 650, "paymentDate": "22-05-2022"},
{"type": "income", "amount": 650, "paymentDate": "22-05-2022"},
{"type": "income", "amount": 650, "paymentDate": "22-05-2022"}
]
我能够在fl图表上显示/反映出来
LineChartBarData(
spots: currentScenario?.transactions
.where((element) => element['type'] == 'income')
.map((e) {
return FlSpot(
DateTime.parse(e['paymentDate']).month
as double,
e['amount']);
}).toList(),
),
LineChartBarData(
spots: currentScenario?.transactions
.where((element) => element['type'] == 'expense')
.map((e) {
return FlSpot(
DateTime.parse(e['paymentDate']).month
as double,
e['amount']);
}).toList(),
)
现在我想做的只是在图表中显示每个月的总数,我试着使用折叠,但我得到的总数不是每个月内的,我认为我做错了,有什么提示吗?
2条答案
按热度按时间chy5wohz1#
lfapxunr2#
复制和害虫此代码并检查它:
在这段代码中,首先按类型筛选事务,然后使用groupBy方法按月份分组,然后使用reduce方法计算每组的总金额。