Home > OS >  Flutter how to delete field from firebase array
Flutter how to delete field from firebase array

Time:12-14

I am trying to delete a field from my firebase array. I am using this code but it deletes then whole activity array.

FirebaseFirestore.instance
                                                  .collection(
                                                      widget.user.user.uid)
                                                  .doc(documentName)
                                                  .update({
                                                "activities":
                                                    FieldValue.delete()

Instead I want to delete a specific field from 'activities' like activities[2]. How can I do to fix this?

CodePudding user response:

To remove an item at a specific index you can use the arrayRemove method from FieldValue

FirebaseFirestore.instance.collection(widget.user.user.uid).doc(documentName).update({
"activities": FieldValue.arrayRemove("itemToRemove")
})

CodePudding user response:

You should try using FieldValue.arrayRemove() method and then pass the value that you want to remove using this :

var val=[];   //blank list for add elements which you want to delete
val.add('$addDeletedElements');

FirebaseFirestore.instance
  .collection(
  widget.user.user.uid)
  .doc(documentName)
  .update({
    "activities":
    FieldValue.arrayRemove(val])
  • Related