SwiftUI Thread 1: Fatal error: No ObservableObject of type SessionStore found

Viewed 1011

I'm creating a SwiftUI app that includes Firebase to enable logging into an account, extremely simple. But when i run this code, the following error "SwiftUI Thread 1: Fatal error: No ObservableObject of type SessionStore found." pops up when calling session.session, can you tell me where the error is in this code ? Thank's guys

import UIKit
import SwiftUI
import Firebase
import Combine

struct ContentView: View {
    
    @EnvironmentObject var session: SessionStore
    
    func getUser () {
        session.listen()
     }
    
    var body: some View{
            VStack {
                if session.session != nil {
                    LoginView()
                }
                else{
                    TabView {
                        ProfilView()
                            .tabItem {
                                Label("Profil", systemImage: "person")
                            }
                        
                        NotificationsView()
                            .tabItem {
                                Label("Notifications", systemImage: "bell")
                            }
                    }
                }
            }
            .onAppear(perform: getUser)
    }
}

struct ContentView_Previews: PreviewProvider {

    static var previews: some View {
        ContentView().environmentObject(SessionStore())
    }
}


Here The SessionStore model :


import Combine
import Foundation
import Firebase

final class User {
    @Published var uid: String
//    var email: String?
    @Published var displayName: String?

    init(uid: String, displayName: String?) {
        self.uid = uid
//        self.email = email
        self.displayName = displayName
    }

}

final class SessionStore: ObservableObject {
    
   @Published var session: User?
   @Published var handle: AuthStateDidChangeListenerHandle?

   func listen () {
       // monitor authentication changes using firebase
       handle = Auth.auth().addStateDidChangeListener { (auth, user) in
           if let user = user {
               // if we have a user, create a new user model
               print("Got user: \(user)")
               self.session = User(
                   uid: user.uid,
                   displayName: user.displayName
               )
           } else {
               // if we don't have a user, set our session to nil
               self.session = nil
           }
       }
   }
}

2 Answers

You need to include .environmentObject(SessionStore()) wherever you instantiate ContentView:

ContentView().environmentObject(SessionStore())

This needs to happen anywhere ContentView is used (eg in the previews and the actual app hierarchy itself, such as coming from SceneDelegate or the WindowGroup if you're using the SwiftUI view lifecycle).

Got this issue today but jnpdx last sentence was essential:

This needs to happen anywhere ContentView is used (eg in the previews and the actual app hierarchy itself...)

Related