1

I have an application that is:

The first view is a list of stores (the list has been read from Firebase successfully) because there is a (List)

The second view is the store details (here, I couldn't read the data from Firebase)

@ObservedObject var viewModel = StoreViewModel() var body: some View { ScrollView { VStack { Text(viewModel.stores.name)}} 

How read this data from Firebase ?

class StoreViewModel: ObservableObject { @Published var stores = [StoreView]() private var db = Firestore.firestore() func fetchData() { db.collection("Stores").addSnapshotListener{(querySnapshot, error)in guard let documents = querySnapshot?.documents else { print("No documents in Firebease") return } self.stores = documents.compactMap { queryDocumentSnapshot -> StoreView? in return try? queryDocumentSnapshot.data(as: StoreView.self) } } } } 
4
  • Please include code and not pictures of code, which can't be copied and pasted, don't work well on mobile devices, etc. You also aren't really clear on what the issue is here. Is it the syntax error you've highlighted? Are you not sure about how to use ForEach here? Commented Apr 24, 2021 at 3:53
  • Yes sure I looked for lessons in reading the data and they all use the List or ForEach, and then they can read the data from Firebase For Ex: List(viewModel.stores){store in Text(store.name) } this way fine can read data but i cant use this : Text(viewModel.store.name) in other view Commented Apr 24, 2021 at 4:03
  • I'm still not sure what your question is then... You can't do Text(viewModel.store.name) because there is no property store on your view model. It looks like you're already clear on how to use the List, as what you said in your comment looks fine. Commented Apr 24, 2021 at 4:12
  • I think there's a question here but it's not clear. Is the code not working? Have you done troubleshooting by stepping through the code, line by line, to see where's it's failing? You said The second view is the store details (here, I couldn't read the data from Firebase) - are you attempting to read the details of a specific store? If so, take a look at the getting started guide Get a single document as that shows how to read one document (a store in your case) Commented Apr 24, 2021 at 13:10

1 Answer 1

1

First, your view model never reads from the database because fetchData() is never called. Therefore, consider calling it automatically in the view model's initializer:

class StoreViewModel: ObservableObject { @Published var stores = [StoreView]() private let db = Firestore.firestore() init() { db.collection("Stores").addSnapshotListener{ (querySnapshot, error) in guard let documents = querySnapshot?.documents else { print("No documents in Firebease") if let error = error { print(error) } return } self.stores = documents.compactMap { queryDocumentSnapshot -> StoreView? in return try? queryDocumentSnapshot.data(as: StoreView.self) } } } } 

Second, stores is an array of StoreView objects, so you must pick one the elements of the array to display any data from it:

Text(viewModel.stores[0].name) // this will display the name of the first store added to the array 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.