0

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

2 Answers 2

1

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();
      },
Sign up to request clarification or add additional context in comments.

Comments

1

Try below code hope its helpful to you.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.