Question: Firebase Firestore Database question: I dont know what im doing wrong, i have a collection in firebase as shown in the picture. I am trying

Firebase Firestore Database question:
I dont know what im doing wrong, i have a collection in firebase as shown in the picture. I am trying to populate the my app witht he 10 entries i have in the collection and list them on my app. Each entry would have the name, description, and price displayed. The formatting and styling is already done is just a matter of switching the hard coded inputs for ones in the firebase collection. Below is the code for my app, contentView, firebaseManger, and shopList for the constructors. Please help!! import SwiftUIstruct ContentView: View { @State var shopListItems =[ShopList]()// Initialize as empty array var body: some View { NavigationView { List(shopListItems){ shopListItem in NavigationLink(destination: ItemView(item: ItemMenu(shopList: shopListItem))){ Text(shopListItem.name)// Display item name in list }}.onAppear { fetchShopListItems { fetchShopListItems in self.shopListItems = fetchShopListItems }}.navigationBarTitle("Start Shopping!", displayMode: .large).toolbar { NavigationLink(destination: CartView(cartItems: [])){ Image(systemName: "cart")}}}}}struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView()}}import SwiftUIimport Firebase // Import Firebase module// Define a struct for ShopList conforming to Identifiablestruct ShopList: Identifiable { let id = UUID() let name: String let description: String let price: Double}// Define ItemMenu and CartItem as beforestruct ItemMenu { let shopList: ShopList init(shopList: ShopList){ self.shopList = shopList } var name: String { return shopList.name } var description: String { return shopList.description } var price: Double { return shopList.price }}struct CartItem: Identifiable { let id = UUID() let item: ItemMenu // Change 'Item' to 'ItemMenu' let quantity: Int}// Modify this function to fetch data from Firestorefunc fetchShopListItems(completion: @escaping ([ShopList])-> Void){ var products =[ShopList]()// Renamed from shopListItems to products let db = Firestore.firestore() let productsRef = db.collection("Products")// Updated collection name productsRef.getDocuments {(querySnapshot, error) in if let error = error { print("Error fetching products: \(error)") completion([])// Call completion with an empty array on error } else { for document in querySnapshot!.documents { let productData = document.data() let name = productData["name"] as? String ??"" let description = productData["description"] as? String ??"" let price = productData["price"] as? Double ??0.0 let product = ShopList(name: name, description: description, price: price) products.append(product)} completion(products)// Call completion with the fetched products array } import SwiftUIimport Firebase@mainstruct ShopAppPt2App: App { @StateObject var firestoreManager = FirestoreManager()// Create an instance of FirestoreManager init(){ FirebaseApp.configure()// Initialize Firebase // Call the function to fetch items from Firestore before the app starts firestoreManager.fetchShopListItems()} var body: some Scene { WindowGroup { ContentView().environmentObject(firestoreManager)// Pass FirestoreManager instance to ContentView }}}import SwiftUIimport Firebaseimport FirebaseFirestoreclass FirestoreManager: ObservableObject {// Conform to ObservableObject @Published var shopListItems =[ShopList]()// Publish shopListItems to trigger updates func fetchShopListItems(){ let db = Firestore.firestore() let productsRef = db.collection("Products") productsRef.getDocuments {(querySnapshot, error) in if let error = error { print("Error fetching products: \(error)")// Handle error if needed } else { var products =[ShopList]() for document in querySnapshot!.documents { let data = document.data()_= document.documentID let name = data["name"] as? String ??"" let description = data["description"] as? String ??"" let price = data["price"] as? Double ??0.0 let product = ShopList(name: name, description: description, price: price) products.append(product)} DispatchQueue.main.async { self.shopListItems = products // Update shopListItems }}}}}
Firebase Firestore Database question: I dont know

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 Accounting Questions!