I currently have a method getReviewsLength() which will get the length of the reviews from firestore for each of the item in _favourites list. However, before the lengths are done adding into reviewsLength list, the method already triggers loadingComplete() method. Is there a way to make sure getReviewsLength() method finishes before triggering loadingComplete()?
I have try with async/await but I cant seem to make it work as i am unsure of where to put there.
getReviewsLength() {
_favourites.forEach((shopname) {
firestore.collection('shops').doc(shopname['shopName']).collection('reviews').get()
.then((value) {
setState(() {
int length = value.size;
reviewsLength.add(length);
});
});
});
loadingComplete();
}
loadingComplete(){
setState(() {
loading = false;
});
print(reviewsLength);
}