【问题标题】:How to delete document from Firestore如何从 Firestore 中删除文档
【发布时间】:2022-01-14 06:14:28
【问题描述】:

不知道如何从 Firestore 中删除文档。

在 PopMenuButton 中,我通过执行以下操作尝试访问 postID 失败:

onSelected: (value) async {
            await FirebaseFirestore.instance.collection('thread').doc(FBCloudStore.  ).delete();
            //await FBCloudStore.deletePostInFirebase(postID);
          },

这是我的 FBCloudStore,我认为我需要更改一些东西。????

将 postID 参数移动到局部变量,然后呢?

class FBCloudStore {
  static Future<void> sendPostInFirebase(String postID, String postContent,
      MyProfileData userProfile, String postImageURL) async {
    String postFCMToken;
    if (userProfile.myFCMToken == null) {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      postFCMToken = prefs.get('FCMToken');
    } else {
      postFCMToken = userProfile.myFCMToken;
    }
    FirebaseFirestore.instance.collection('thread').doc(postID).set({
      'postID': postID,
      'userName': userProfile.myName,
      'userThumbnail': userProfile.myThumbnail,
      'postTimeStamp': DateTime
          .now()
          .millisecondsSinceEpoch,
      'postContent': postContent,
      'postImage': postImageURL,
      'postLikeCount': 0,
      'postCommentCount': 0,
      'FCMToken': postFCMToken
    });
  }

//Should it be like this?????
  deletePostInFirebase(String postID) async {
    await FirebaseFirestore.instance.collection('thread').doc(postID).delete();
  }
}

这是带有 PopupMenuButton 的线程项。

  class ThreadItem extends StatefulWidget {
  final BuildContext parentContext;
  final DocumentSnapshot data;
  final MyProfileData myData;
  final ValueChanged<MyProfileData> updateMyDataToMain;
  final bool isFromThread;
  final Function threadItemAction;
  final int commentCount;

  ThreadItem(
      {this.data,
      this.myData,
      this.updateMyDataToMain,
      this.threadItemAction,
      this.isFromThread,
      this.commentCount,
      this.parentContext});

  @override
  State<StatefulWidget> createState() => _ThreadItem();
}

class _ThreadItem extends State<ThreadItem> {
  MyProfileData _currentMyData;
  int _likeCount;

  @override
  void initState() {
    _currentMyData = widget.myData;
    _likeCount = widget.data['postLikeCount'];
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return Row(
      children: <Widget>[
        PopupMenuButton<int>(
          itemBuilder: (context) => [
            PopupMenuItem(
              value: 1,
              child: Row(
                children: [
                  Text("Delete post"),
                ],
              ),
            ),
          ],
          initialValue: 1,
          onCanceled: () {
            print("You have canceled the menu.");
          },
          onSelected: (value) async {
            print('delete');
            //TODo how to delete post?
            //await FBCloudStore.deletePostInFirebase(postID);
            //await FirebaseFirestore.instance.collection('thread').doc(FBCloudStore.  ).delete();
          },
        ),
      ],
    );
  }
}

【问题讨论】:

    标签: flutter google-cloud-firestore


    【解决方案1】:

    在您的 onSelected 函数中执行以下操作:

    onSelected: (value) async {
                print('delete');
                //TODo how to delete post?
                //await FBCloudStore.deletePostInFirebase(postID);
                FirebaseFirestore.instance.collection("thread").doc(widget.data['postID']).delete();
          },
    

    【讨论】:

      【解决方案2】:

      试试下面的代码希望对你有帮助。

       onTap: ()async{  
         await FirebaseFirestore.instance
               .collection('collection name')
               .doc(your id here)
               .delete(); 
        }
      

      【讨论】:

        猜你喜欢
        • 2020-09-01
        • 2020-12-11
        • 2021-10-16
        • 1970-01-01
        • 2018-06-18
        • 2021-01-13
        • 2020-11-23
        • 2021-09-13
        • 1970-01-01
        相关资源
        最近更新 更多