After getting the same data, I want to delete a specific field in the Array inside a document
[Execution][2][2]: https://i.stack.imgur.com/Ir687.jpg
my code
deleteItemToCart(String? foodItemId, BuildContext context, int quantity)
{
String itemID = foodItemId!+ ":$quantity";
FirebaseFirestore.instance
.collection("users")
.doc(firebaseAuth.currentUser!.uid)
.update({
'userCart.$itemID' : FieldValue.delete(),
}).then((value)
{
Fluttertoast.showToast(msg: "Item Added Successfully.");
print("ItemID " + itemID);
//update the badge
Provider.of<CartItemCounter>(context, listen: false).displayCartListItemsNumber();
});
}
To delete an item from an array, you'll want to use FieldValue.arrayRemove
operator as shown in the documentation on updating elements in an array .
If I have if correctly, that'd be:
.update({
'userCart' : FieldValue.arrayRemove([itemID]),
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.