FieldValue.arrayRemove()从firestore中的数组中删除所有对象



我正在创建一个扑动应用程序,有一个包含姓名,电子邮件,uid和购物车的用户模型。我已经创建了一个扩展GetxController的CartController来向购物车中添加商品并从购物车中删除特定商品。但是,我需要创建一个函数,该函数从包含购物车项目作为对象的购物车中删除所有项目。我知道可以使用FieldValue.arrayRemove()删除特定的购物车项目,但我想知道如何从数组中删除所有元素。

您实际上是在尝试将cart数组设置为空数组。您可以像文档中那样使用update方法:

final docRef = db.collection("collection").doc("docId");
docRef.update({"cart": []}).then(
(value) => print("DocumentSnapshot successfully updated!"),
onError: (e) => print("Error updating document $e"));

可以对数组变量使用。clear()

FieldValue.clear()

最新更新