I am making a simple app for a small business using Firebase. So far the data from the cart gets read without any issues, but when I try to iterate through it to add the subtotal I get nothing. This is the code for this part. Let me know if you need any more information.
@Published var data = [cart]()
@Published var subtotal: Float = 0
init() {
let db = Firestore.firestore()
db.collection("cart").addSnapshotListener { snap, error in
if error == nil {
if let snap = snap {
DispatchQueue.main.async {
self.data = snap.documents.map { doc in
return cart(
id: doc.documentID,
item_name: doc["name"] as? String ?? "",
item_price: doc["price"] as? NSNumber ?? 0,
item_description: doc["description"] as? String ?? "",
item_image: doc["image"] as? String ?? "")
}
}
}
}
}
for i in self.data {
self.subtotal += Float(i.item_price)
}