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) } } } }
https://stackoverflow.com/questions/67239171/i-cannot-read-data-from-firebase-without-a-list-or-foreach April 24, 2021 at 11:51AM
没有评论:
发表评论