I have a function called getAvctUODoss() which is supposed to return a Future<double> but it always return 0.0. If I add a print('sum'); in my forEach loop, I see that the code works, it's just for the return of it that I have some difficulties. I need the result of this function in another function but, because I can't go further because of this wrong return. I think my mistake is in the declaration of sum as a double but I have not idea how to fix this. I searched on Stack Overflow but I only found codes which print the values inside the forEach loop.
Thank you in advance for your precious help.
Here is the code of my two functions :
Future<double> getAvctUODoss(String numDoss) async {
double sum = 0.0; // I think the problem is in this declaration
affaireCollection
.document(numDoss.substring(0, 7))
.collection('dossiers')
.document(numDoss)
.collection('rp')
.getDocuments()
.then((querySnapshot) {
querySnapshot.documents.forEach((documentSnapshot) async {
sum += await documentSnapshot.data['avct'] *
(await documentSnapshot.data['UO_AT'] +
await documentSnapshot.data['UO_CH']);
});
});
return sum; //return always 0.0
}
Future updateAvctDoss(String numDoss) async {
await affaireCollection
.document(numDoss.substring(0, 7))
.collection('dossiers')
.document(numDoss)
.updateData({
'avct': await getAvctUODoss(numDoss) / await getTotalUODoss(numDoss)
});
}