Flutter中日报表ObjectList到MapList的转换

kmbjn2e3  于 2023-01-31  发布在  Flutter
关注(0)|答案(2)|浏览(157)

我已经创建了一个将TransactionList转换为MapList的方法,用于每日收入和每日支出的报告。
似输出
[ {“日期”:28-01-23,“总收入”:300.00,“总支出”:300.00,“余额”:0.00,
}、{ ...}、]
这是交易的数据

List<Transaction> transactions=[
  Transaction(date: DateTime(2023,01,18), isExpense: true, amount: 100.00,),
  Transaction(date: DateTime(2023,01,18), isExpense: true, amount: 200.00,),
  Transaction(date: DateTime(2023,01,18), isExpense: false, amount: 300.00,),
  Transaction(date: DateTime(2023,01,19), isExpense: false, amount: 200.00,),
  Transaction(date: DateTime(2023,01,19), isExpense: false, amount: 100.00,),
];

我的方法工作得很好,但我认为它不是正确的编码,所以我想用一些先进的方式实现这个方法...

List<Map<String, dynamic>> dailyreport() {
    
    //grouping data based on date field of transactions
    var maplist = groupBy(transactions, (Transaction element) => element.date);

    List<Map<String, dynamic>> reportlist = [];
    
    
    
    
    //loop for each key
    for (var x in maplist.keys) {

double sum_expenses = 0;
    double sum_income = 0;
      
      //getting transaction based on key value
      List<Transaction> trans = maplist[x] as List<Transaction>;
      for (int i = 0; i < trans.length; i++) {
        if (trans[i].isExpense)
          sum_expenses = sum_expenses + trans[i].amount;
        else
          sum_income = sum_income + trans[i].amount;
      }
      
      //adding map to reportlist
      reportlist.add({
        'date': x,
        'expenses': sum_expenses,
        'income': sum_income,
        'balance': sum_income - sum_expenses,
      });
    }
    return reportlist;
  }
yhqotfr8

yhqotfr81#

下面是另一种方法,同样使用groupBy,然后折叠结果列表值:

Map<String, dynamic> emptyReport(DateTime d) =>
  {
    "date": d,
    "expenses": 0.0,
    "income": 0.0,
    "balance": 0.0,
  };

Map<String, dynamic> addToReport(Map<String, dynamic> report, Transaction t) => report["date"] == t.date ?
  {
    ...report,
    if (t.isExpense) 
      ...{
        "expenses": t.amount + report["expenses"],
        "balance": report["balance"] - t.amount,
      }
    else
      ...{
        "income": t.amount + report["income"],
        "balance": report["balance"] + t.amount,
      }
  } :
  report; // or handle the case the dates don't match however you want. but that shouldn't happen the way it is used below

List<Map<String, dynamic>> dailyreport(List<Transaction> transactions) =>
  groupBy(transactions, (t) => t.date).map((date, ts) => MapEntry(
    date,
    ts.fold(
      emptyReport(date),
      addToReport
  ))).values.toList();
eufgjt7s

eufgjt7s2#

下面是一个循环的try:

List<Map<String, dynamic>> dailyreport(List<Transaction> transactions) {
    Map<DateTime, Map<String, dynamic>> resultsByDate = {};
    for(var t in transactions) {
        var curr = resultsByDate[t.date] ?? {
            "date": t.date,
            "expenses": 0.0,
            "income": 0.0,
            "balance": 0.0,
        };

        resultsByDate[t.date] = {
          ...curr,
          if (t.isExpense) 
            ...{
              "expenses": t.amount + curr["expenses"],
              "balance": curr["balance"] - t.amount,
            }
          else
            ...{
              "income": t.amount + curr["income"],
              "balance": curr["balance"] + t.amount,
            },
        };      
    }

    return resultsByDate.values.toList();
}

相关问题