'what is the reason for this error and how to solve it
add method produce error saya his expression has a type of 'void' so its value can't be used.
Future addtocart({
String? productID,
BuildContext? context,
}) async {
List<String>? cartitems = ["gabbage"];
await _firestore
.collection("Users")
.doc(_currentUser!.uid)
.collection("Cart")
.doc(_currentUser!.uid)
.get()
.then((doc) {
if (doc.data()!["UserCart"].contains(productID)) {
Fluttertoast.showToast(
msg: 'Product is already in the cart',
gravity: ToastGravity.CENTER,
);
} else {
_firestore
.collection("Users")
.doc(_currentUser!.uid)
.collection("Cart")
.doc(_currentUser!.uid)
.update({"UserCart": cartitems.add(productID!)});
}
});
}
}
Solution 1:[1]
Instead of cartitems.add(productID!), you can do FieldValue.arrayUnion(productID!).
You can find the documentation of FieldView here: https://pub.dev/documentation/cloud_firestore/latest/cloud_firestore/FieldValue-class.html
Solution 2:[2]
cartitems.add(productID!);
It's not returning anything so how can you assign it to a map
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Kai - Kazuya Ito |

