Flutter 排序、Map、减少Map列表

hfyxw5xn  于 2023-02-05  发布在  Flutter
关注(0)|答案(2)|浏览(203)

嘿,我有这个来自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(),
                       )

现在我想做的只是在图表中显示每个月的总数,我试着使用折叠,但我得到的总数不是每个月内的,我认为我做错了,有什么提示吗?

chy5wohz

chy5wohz1#

void main() {
  var data = [
    {"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"}
  ];
  List<Transactions> transactions = [];
  Set<int> eachMonth = {};

  void parsJsonDataToModel() {
    for (var value in data) {
      transactions.add(Transactions.fromJson(value));
    }
  }

  void geEachMonthsFromData(List<Transactions> transactions) {
    for (var element in transactions) {
      eachMonth.add(element.formattedPaymentDate!.month);
    }
  }

  void getTotalOfAmountMonthWise() {
    for (var month in eachMonth) {
      print(
          "Month: $month : Total: ${transactions.where((element) => element.formattedPaymentDate!.month == month).fold<int>(0, (previousValue, element) => previousValue += element.amount!)}");
    }
  }

  parsJsonDataToModel();
  geEachMonthsFromData(transactions);
  getTotalOfAmountMonthWise();
}

class Transactions {
  Transactions({
    required this.type,
    required this.amount,
    required this.paymentDate,
  });

  String? type;
  int? amount;
  String? paymentDate;
  DateTime? formattedPaymentDate;

  Transactions.fromJson(Map<String, dynamic> json) {
    type = json['type'];
    amount = json['amount'];
    paymentDate = json['paymentDate'];
    formattedPaymentDate = DateFormat("dd-MM-yyyy").parse(json['paymentDate']);
  }
}
lfapxunr

lfapxunr2#

复制和害虫此代码并检查它:

LineChartBarData(
  spots: currentScenario?.transactions
      .where((element) => element['type'] == 'income')
      .groupBy((e) => DateFormat("dd-MM-yyyy").parse(json['paymentDate'].month)
      .map((month, transactions) {
        return FlSpot(
            month.toDouble(),
            transactions.map((e) => e['amount']).reduce((a, b) => a + b));
      })
      .values
      .toList(),
),
LineChartBarData(
  spots: currentScenario?.transactions
      .where((element) => element['type'] == 'expense')
      .groupBy((e) => DateFormat("dd-MM-yyyy").parse(json['paymentDate'].month)
      .map((month, transactions) {
        return FlSpot(
            month.toDouble(),
            transactions.map((e) => e['amount']).reduce((a, b) => a + b));
      })
      .values
      .toList(),
)

在这段代码中,首先按类型筛选事务,然后使用groupBy方法按月份分组,然后使用reduce方法计算每组的总金额。

相关问题