Question: I dont know why my data is not getting read from firebase. Here is my viewControler, contentView, and strut for product. struct ContentView: View {

I dont know why my data is not getting read from firebase. Here is my viewControler, contentView, and strut for product. struct ContentView: View {
@ObservedObject var viewController = ViewController()
var body: some View {
VStack {
Button("Fetch Data"){
viewController.getData()
}
List(viewController.list){ product in
VStack(alignment: .leading){
Text("Name: \(product.name)")
Text("Description: \(product.description)")
Text("Price: $\(product.price)")
}
}
}
} 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 = data["id"] as? String ??""
return Product(id: id, name: name, description: description, price: price)
}
}
}
}
}
}ShopAppDemo1-...
Read and Write Dat...
Perform simple an..
BoundlessArchive..
Cloud Firestore Ge...
ChatGPT
Untitled document...
Firebase
ShopAppDemo1
Cloud Firestore
Project Overview
Data
Rules
Indexes
Usage
Extensions
Project shortcuts
Firestore Database
Storage
What's new
Products
M6lOfeA9hllmz...
More in Google Cloud
Release Monit...
NEW
Product categories
Build
Release & Monitor
Analytics
Engage
I dont know why my data is not getting read from

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!