【问题标题】:How can I delete particular document from cloud firestore using flutter? [closed]如何使用颤振从云 Firestore 中删除特定文档? [关闭]
【发布时间】:2020-12-16 11:06:34
【问题描述】:

在使用存储在云 Firestore 中的颤振处理数据方面,我需要帮助。我创建了一个功能,当您点击图标时添加数据并在您点击 2 次时删除。

这是我的代码:

void switcherIsFavSalon() async {
  var firebaseUser = await FirebaseAuth.instance.currentUser();
  var docRef = Firestore.instance
      .collection('customers')
      .document(firebaseUser.uid)
      .collection('favSalons');

  if (isFavSalon == false) {
    setState(() {
      favIcon = 'assets/icons/heart_filled.png';
      isFavSalon = true;
      docRef.add({
        "salonName": widget.salonName,
        "workTime": widget.workTime,
        "location": widget.location,
        "rating": widget.rating
  // here is add method
      });
    });
  } else {
    setState(() {
      favIcon = 'assets/icons/heart_border.png';
      isFavSalon = false;
      docRef. //i need delete method here
    });
  }
}

谁能帮助我,因为我是新手。

【问题讨论】:

    标签: firebase flutter google-cloud-firestore


    【解决方案1】:
            void switcherIsFavSalon() async {
              var firebaseUser = await FirebaseAuth.instance.currentUser();
              var docRef = Firestore.instance
                  .collection('customers')
                  .document(firebaseUser.uid)
                  .collection('favSalons');
            
              if (isFavSalon == false) {
                setState(() {
                  favIcon = 'assets/icons/heart_filled.png';
                  isFavSalon = true;
                 await docRef.add({
                    "salonName": widget.salonName,
                    "workTime": widget.workTime,
                    "location": widget.location,
                    "rating": widget.rating
              // here is add method
                  });
                });
              } else {
                setState(() {
                  favIcon = 'assets/icons/heart_border.png';
                  isFavSalon = false;
                  });
    docRef=await Firestore.instance.collection('customers').document(firebaseUser.uid)
                  .collection('favSalons');
           await docRef.getDocuments().then((value) {  
                                                 value.documents.forEach((element) {
    if(element['salonName']==widget.salonName)
        await docRef.document(element.documentID).delete();
          });
         });
    }
    

    【讨论】:

    • 有错误提示无法将 List 分配给参数类型 Futute,我该怎么办?
    • 现在它说 QuerySnapshot 不能分配给 List
    • 进行了更多编辑。让我知道它是否有效
    • 在 docList 中的 getter 'data' 没有为类型 'Future' 定义。
    • “QuerySnapshot”类型的值不能分配给“Future”类型的变量。还有这个
    猜你喜欢
    • 2019-01-09
    • 2020-08-05
    • 2020-12-23
    • 2021-08-11
    • 2023-01-29
    • 1970-01-01
    • 2021-04-14
    • 1970-01-01
    相关资源
    最近更新 更多