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
Here’s a try with one loop:
Here’s another approach, also using
groupBy
as you were, and then folding the resulting list values: