I am still battling a way to make a simple chart that shows aggregated amount of payments (transactions) monthly - reference to this post: https://community.flutterflow.io/ask-the-community/post/simple-chart-that-shows-aggregated-transactions-per-month-vq93degvPO1LgJA
Have created this custom function, that accepts list of documents returned from the query firestore, then does aggregation, and returns JSON.
List<dynamic>? getMonthlyPaymentTotals(List<PaymentsTestRecord> paymentsList) {
/// MODIFY CODE ONLY BELOW THIS LINE
// Create a map to store monthly total payments
Map<String, double> monthlyTotals = {};
// Iterate through each payment document
for (PaymentsTestRecord payment in paymentsList) {
// Extract the payment amount and created date
double amount = payment.amount ?? 0.0;
DateTime createdAt = payment.createdAt ?? DateTime.now();
// Get the month and year from the created date
String month = DateFormat('MM').format(createdAt);
// Update the monthly total payments map
if (monthlyTotals.containsKey(month)) {
// Use the null assertion operator '!' to assert that the value is not null
monthlyTotals[month] = monthlyTotals[month]! + amount;
} else {
monthlyTotals[month] = amount;
}
}
// Convert the monthly totals map to a list of JSON objects
List<dynamic> monthlyTotalsList = monthlyTotals.entries.map((entry) {
return {
'month': entry.key,
'paymentsAmount': entry.value,
};
}).toList();
return monthlyTotalsList;
/// MODIFY CODE ONLY ABOVE THIS LINE
}
The result is expected to be like this
[
{'month': '09', 'paymentsAmount': 300.0},
{'month': '10', 'paymentsAmount': 200.0},
]
So what I've done:
1) on the chart widget added backend Query Collection
2) Selected in Chart Data Data Source - Firestore Documents
3) Selecting "Custom Function" as Data - getMonthlyPaymentTotals
4) Selecting in the function arguments - the paymentsList (as I am getting it from the backend query)
It shows "Current variable is not valid"
I believe I am on the right path, but got stuck in the instruments of FF to figure out how to get it nailed.