Hi everyone,
I'm trying to code a custom action ('downloadCollectionAsCSV') to export firestore records (transactions) to a CSV file.
I'm able to export the records to CSV. One of the columns in my transactions collection is a document reference to a categories collection; I don't want to export the reference but the description from the categories collection, so I have another custom action ('getcategorydesc') which takes the reference and returns the description, and I then use this in the action 'downloadCollectionAsCSV'.
When I export the CSV, I don't get the category description but instead get the text 'Instance of '_Future<String>'. Can anyone suggest why that would be?
I've manually tested the 'getcategorydesc' action, and it returns the category description, but it doesn't seem to work when it is reference in the other action (highlighted in bold below). I've put my code below. Any help appreciated.
downloadCollectionAsCSV
Future downloadCollectionAsCSV(List<TransactionRecord>? docs) async {
docs = docs ?? [];
String companyName = "mint-coaching.com";
String companyAddress = "";
String header = "$companyName,$companyAddress\n\n";
String fileContent = header + "Transaction Date, Type, Amount, Currency, Note";
docs.asMap().forEach((index, record) => fileContent = fileContent + "\n" +
DateFormat('dd-MM-yyyy').format(record.transactionDate!) + "," +
record.type.toString() +"," +
record.amount.toString() + "," +
record.currency.toString() + "," +
record.note.toString() + "," +
getcategorydesc(record.category).toString());
final fileName = "FF" + DateTime.now().toString() + ".csv";
// Encode the string as a List<int> of UTF-8 bytes
var bytes = utf8.encode(fileContent);
final stream = Stream.fromIterable(bytes);
return download(stream, fileName);
}
getcategorydesc
Future<String> getcategorydesc(DocumentReference? categoryref) async {
final categorydoc = await categoryref?.get();
final categorymap = categorydoc?.data() as Map<String, dynamic>;
return categorymap['Category'] ?? 'NULL' as String;