When I have a Future function that have nested future functions inside, the result doesn't wait for function to complete, here is a sample of code to get data from firestore:
Previous variables: DocumentReference idoc and List sids
Future<bool> check() async {
bool result = true;
await idoc.get().then((value) {
if (!value.data['av']) {
result = false;
} else {
sids.forEach((sid) async {
DocumentReference jdoc = idoc.collection('items').document(sid);
await jdoc.get().then((jdocValue) {
if (!jdocValue.data['av']) {
result = false;
}
});
});
}
});
return result;
}
When I call check(), even using then:
check().then( (value) => print(value.toString()) );
I just get 'false' if the first check is true, I mean if "idoc.data['av']" in firestore is false, but, if this condition is false and I entered in the 'else' to evaluate each document inside 'items' collection, then if any of them is false I get 'true' calling check().
I want to wait for the hole evaluation of that function and get 'false' as a result to use it if any document of 'items' is false.