UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Firebase Problem with ViewModel isEqualTo..

Forums > SwiftUI

Im searching in firebase to a model called anuncios ((uid) with the same user (id).

SearchAnuncioUsuarioViewModel:

import SwiftUI
import Firebase

class SearchAnuncioUsuarioViewModel: ObservableObject {

    let user: User
    @Published var userAnuncios = [Anuncio]()

    init(user:User) {
        self.user = user
        fetchUserAnuncios()
    }

    func fetchUserAnuncios() {
        COLLECTION_ANUNCIOS.whereField("uid", isEqualTo: user.id).getDocuments { snapshot, _ in

            guard let documents = snapshot?.documents else { return }
            documents.forEach { document in
                self.userAnuncios = documents.map({ Anuncio(dictionary: $0.data()) })
            }
        }
    }

}

In Content View. I have a tabview called my anuncios, and this code @EnvironmentObject var viewModel3: SearchAnuncioUsuarioViewModel ... MeusAnunciosView(user: viewModel3.user) ....

and

By Anuncios View, where the anuncios are called:

import SwiftUI

struct MeusAnunciosView: View {

    private let user: User
    @ObservedObject private var viewModel3: SearchAnuncioUsuarioViewModel

    init(user: User) {
        self.user = user
        self.viewModel3 = SearchAnuncioUsuarioViewModel(user: user)
    }

    var body: some View {
        ZStack {
        NavigationView {
            ScrollView(.vertical, showsIndicators: false) {

                VStack {
                    Text("Meus Anuncios:")
                }

                VStack {
                    ForEach(viewModel3.userAnuncios) { anuncio in // USERS é o MODEL OBJECT
                        Text(anuncio.titulo)

                    }

                }

            }.navigationBarHidden(true)

        }
        }
    }

}

When I run the code, this error apper: Thread 1: Fatal error: No ObservableObject of type SearchAnuncioUsuarioViewModel found. A View.environmentObject(_:) for SearchAnuncioUsuarioViewModel may be missing as an ancestor of this view. (In Content View)

How can I Solve it??? Thank You!!

2      

Please, help me =)

2      

Please, someone can help me ?

If you dont understand i can explain, sorry me english is not so good.

2      

You could look at these to see if their explanations can help you.

This explains that TabViews may have their own separate environments. No ObservableObject of type x found

Alternative discussion, and possible solution for your case, maybe. No ObservableObject found - Ancestor might be missing

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.