I'm still learning Dart and OOP and can't quite get this right. The problem is that _batch.addAll() executes before _invDetails is populated from the inner ForEach because of the delay from the query. How do I structure this properly?
Future<Response> exportAccpac() async {
final Map<String, dynamic> _inv = {'inv': []};
final Map<String, dynamic> _invDetails = {'invDet': []};
final Map<String, dynamic> _batch = {};
final _qExpenses = Query<UserCompany>(context);
_qExpenses.where((e) => e.company.id).equalTo(companyId);
_qExpenses
.join(object: (e) => e.user)
.join(set: (u) => u.expense)
.where((d) => d.date)
.lessThanEqualTo(date);
final _expenseList = await _qExpenses.fetch();
_expenseList.forEach((e) {
_inv['inv'].add(
{
// a bunch of stuff here using e
},
);
e.user.expense.forEach((userExpense) async {
final _qGlCode = Query<GlCode>(context)
..where((g) => g.user_type.id).equalTo(_userTypeId)
..where((g) => g.company.id).equalTo(companyId)
..where((g) => g.expense_type.id).equalTo(userExpense.expense_type.id);
final _glCodes = await _qGlCode.fetch();
_invDetails['invDet'].add({
// a bunch of stuff here using glCodes
});
});
});
_batch..addAll(_inv)..addAll(_invDetails);
}