addExpense function
Implementation
Future addExpense(Map<String, dynamic> payload) async {
String userID = await getUserID();
ResultList<RecordModel> records = await getRecords("expenses");
String currentDate = "${DateTime.now().month}/${DateTime.now().year}";
Map recordsUnderThisMonth = {};
String recordID= "";
if (records.items.isNotEmpty) {
records.items.forEach((result) {
recordsUnderThisMonth = result.data['records'];
print(recordsUnderThisMonth);
recordID=result.id;
});
// Check if the current date key exists in the records
if (recordsUnderThisMonth.containsKey("6/2024")) {
// Add the new record to the existing list
recordsUnderThisMonth["6/2024"].add(payload);
} else {
// Create a new list for the current date key
recordsUnderThisMonth["6/2024"] = [payload];
}
final result = await pocketBase.collection("expenses").update(
recordID,
body: {
"userID": userID,
"records": recordsUnderThisMonth,
},
);
print("---$result");
} else {
await pocketBase.collection("expenses").create(body: {
"userID": userID,
"records": {currentDate: [payload]},
});
}
}