Home > database >  FieldValue.arrayRemove() to remove all objects from an array in firestore
FieldValue.arrayRemove() to remove all objects from an array in firestore

Time:06-23

I am creating a flutter app and there is a User model which contains name,email,uid and cart. I have created a CartController extends GetxController to add items to cart and remove a particular item from cart. However, I need to create a function which deletes all the items from the cart which contains cart item as objects. I know that a particular cart item can be removed using FieldValue.arrayRemove() but I want to know how can i delete all the elements from the array.

CodePudding user response:

You can use the .clear() on the array variable.

Try

FieldValue.clear()

CodePudding user response:

You are essentially trying to set cart array to an empty array. You can just use update method as in the documentation:

final docRef = db.collection("collection").doc("docId");

docRef.update({"cart": []}).then(
    (value) => print("DocumentSnapshot successfully updated!"),
    one rror: (e) => print("Error updating document $e"));
  • Related