[英]Loop through all documents in a Firestore collection and add those documents to another collection in Flutter
[英]Loop through all firestore documents in a collection AND update them in flutter
我需要遍歷“練習”集合中的所有文檔並使用新值更新它們。 我寫了一個有效的函數,但它是一個低效的解決方案。 這是整個功能:
final thisWorkout = await FirebaseFirestore.instance
.collection('workouts')
.doc(widget.docRef!.id);
final allExercises = await thisWorkout.collection('exercise').get();
thisWorkout.update({
'name': workoutName,
});
List allIds = [];
allExercises.docs.forEach((DocumentSnapshot value) {
allIds.add(value.reference.id);
});
for (var i = 0; i < exercises.length; i++) {
thisWorkout.collection('exercise').doc(allIds[i]).set(exercises[i]);
}
這是功能效率低下的部分:
List allIds = [];
allExercises.docs.forEach((DocumentSnapshot value) {
allIds.add(value.reference.id);
});
for (var i = 0; i < exercises.length; i++) {
thisWorkout.collection('exercise').doc(allIds[i]).set(exercises[i]);
}
有沒有辦法只遍歷每個文檔並更新它而不存儲所有 id 然后為更新運行單獨的 for 循環?
你是這個意思?
allExercises.docs.forEach((DocumentSnapshot value) {
thisWorkout.collection('exercise').doc(value.reference.id).set(exercises[i]);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.