skip to Main Content

I Have created a method that converts TransactionList to MapList for report of daily income and daily expenses..

output like

[
{
‘date’:28-01-23,
‘totalIncome’:300.00,
‘totalExpense’:300.00,
‘balance’:0.00,

},
{
….
},
]

here are the data of Transaction


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,),
];


My method is working well but I think it is not proper coding, so I want to implement this method with some advance way….

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;
  }

2

Answers


  1. Here’s a try with one loop:

    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();
    }
    
    Login or Signup to reply.
  2. Here’s another approach, also using groupBy as you were, and then folding the resulting list values:

    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();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search