Question: I dont know why my Database in firestore is not reading. Below is my code for the ViewController, struct for Product, and contentView. Please help.

I dont know why my Database in firestore is not reading. Below is my code for the ViewController, struct for Product, and contentView. Please help. struct ContentView: View {
@ObservedObject var viewController = ViewController()
var body: some View {
List(viewController.list, id: \.id){ product in
VStack(alignment: .leading){
Text(product.name)
.font(.headline)
Text(product.description)
.font(.subheadline)
Text("$\(product.price)")
.font(.subheadline)
.foregroundColor(.gray)
}
}
.onAppear {
self.viewController.getData()
}
}
} struct Product: Identifiable {
let id: String
let name: String
let description: String
let price: Double
} class ViewController: ObservableObject {
@Published var list =[Product]()
func getData(){
let db = Firestore.firestore()
db.collection("Products").getDocuments { snapshot, error in
if let error = error {
print("Error getting documents: \(error.localizedDescription)")
return
}
if let snapshot = snapshot {
DispatchQueue.main.async {
self.list = snapshot.documents.map { document in
let data = document.data()
let name = data["name"] as? String ??""
let description = data["description"] as? String ??""
let price = data["price"] as? Double ??0.0
// Generate a unique identifier for each product
let id = document.documentID
return Product(id: id, name: name, description: description, price: price)
}
}
}
}
}
} Ive also put a screenshot of my DateBase
I dont know why my Database in firestore is not

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!