首页 > 解决方案 > 如何在'Firestore DocumentSnapshot'的for循环中只调用一次函数?

问题描述

我正在尝试从firestore使用中删除文档:

 FirebaseFirestore.instance
      .collection("Data")
      .where('user_id', isEqualTo: uid)
      .get()
      .then((snapshot) {
    for (DocumentSnapshot ds in snapshot.docs) {
      ds.reference.delete().then((value) {
        print("document deleted");
        moveToNextDeletion(); // <-The function shold be called only once
      });
    }
  });

因此,一旦删除了所有文档,我就想调用moveToNextDeletion();函数,但它会一直调用,直到所有文档都被删除(导致 for 循环)。

删除所有文档后,有什么方法可以调用该函数?

标签: firebaseflutterdartgoogle-cloud-firestore

解决方案


使用while循环而不是for循环希望它会起作用


推荐阅读