Alex K
Alex K

Reputation: 81

How to delete document from Firestore

Can't figure out how to delete document from Firestore.

Inside PopMenuButton I am unsuccessfully trying access postID by doing the following:

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

Here is my FBCloudStore, where I think I need to change something.🤔

Move postID parameter to local variables and then what?

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();
  }
}

And this is the thread item with 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();
          },
        ),
      ],
    );
  }
}

Upvotes: 0

Views: 51

Answers (2)

Ravindra S. Patil
Ravindra S. Patil

Reputation: 14775

Try below code hope its helpful to you.

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

Upvotes: 1

Muhammad Hussain
Muhammad Hussain

Reputation: 1244

In your onSelected function do the following:

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

Upvotes: 1

Related Questions