Home > Blockchain >  'self' used in method call 'fetchCurrentUser' before all stored properties are i
'self' used in method call 'fetchCurrentUser' before all stored properties are i

Time:11-09

class UserViewModel: ObservableObject {
    @Published var user: User?
    @Published var isLogOut: Bool
    
    init() {
        fetchCurrentUser() // 'self' used in method call 'fetchCurrentUser' before all stored properties are initialized

        isLogOut = FirebaseManager.shared.auth.currentUser?.uid == nil
    }

    func fetchCurrentUser() {
        guard let uid = FirebaseManager.shared.auth.currentUser?.uid else { return }
        
        FirebaseManager.shared.firestore.collection("users").document(uid).getDocument { snapshot, err in
            guard let data = snapshot?.data() else {return}
            
            self.user = User(uid: data["uid"] as? String ?? "", email:data["email"] as? String ?? "", profilePicURL: data["url"] as? URL ?? URL(string: ""))
        }
    }

I was trying to create function for init to fetch the data from the firesore. Why do I get this error if my user variable is optional? Is there another way to init user using data from firebase?

CodePudding user response:

This also has to be optional

@Published var isLogOut: Bool?
  • Related