Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.🤔

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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();
      },
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading