elements Getting Random in for Each并从firestore获取数据



fireDetail.forEach((element) async {
await checkEach(element);

});
}
checkEach(element) async {
print(element.path);
await Firestore.instance
.document('${element.path}/likedBy/$currentUser')
.get()
.then((value) => print(element.path));   
}


/颤振(6906(:火灾/c0829bb0-2dce-11eb-be17-6debf3d459db
I/flutter(6906(:火灾/b9a45680-2dce-11eb-b289-d9b50dcbb605
I/flutter(6906(:火灾/89aa14b0-2dce-11eb-b507-672e33a1029
I/flutter(6906(:火灾/6ebfcd0-2dce-11eb-8a61-9909929ac404

//下面是的订单。然后
I/flutter(6906(:fire/89aa14b0-2dce-11eb-b507-672e33a10129
I/flutter
可以看出,每个发送的元素与内部打印的元素不同。然后(值(..因此,进一步的代码会变得混乱。如何解决这个问题?

对Firestore的调用是异步发生的。当您的await Firestore.instance使checkEach等待时,它不会同步对checkEach的多个调用,因为您没有返回任何内容。

跨调用同步的最简单方法是在对forEach的调用上对await进行同步,从而使其从Firestore:返回Future

fireDetail.forEach((element) async {
await checkEach(element);     
});
checkEach(element) async {
return Firestore.instance
.document('${element.path}/likedBy/$currentUser')
.get()
.then((value) => print(element.path));   
}

我回答得太晚了,但这就是我解决这个问题的方式


checkIsLiked(List fireDetail) async {

for (var i in fireDetail) {
await checkEach(i);
}
}
checkEach(element) async {
print(element.path);
await Firestore.instance
.document('${element.path}/likedBy/$currentUser')
.get()
.then((value) => print(element.path));   
}

我用典型的for循环替换了forEach。

相关内容

最新更新