简体   繁体   中英

How can I display name of User object in ProfileViewController

I need to display user.username on Text in ProfileView but I got error when I try to fill current user with User. I have to get User in currentUser var.

import Foundation
import FirebaseAuth
import Firebase

class AuthViewModel: ObservableObject
{
    @Published var userSession: FirebaseAuth.User?
    @Published var currentUser: User?
    
    private var tempUserSession: FirebaseAuth.User?
    private let service = UserService()
    
    init()
    {
        self.userSession = Auth.auth().currentUser
    }
    
    func login(withEmail email: String, password: String)
    {
        Auth.auth().signIn(withEmail: email, password: password) { authResult, error in
            if let e = error
            {
                print(e.localizedDescription)
            }
            else
            {
                guard let user = authResult?.user else {return}
                self.userSession = user
                guard let uid = self.userSession?.uid else { return }
                self.service.fetchUser(withUid: uid)
                print("Did User log IN")
            }
        }
    }
    func fetchUser()
    {
        guard let uid = self.userSession?.uid else { return }
        service.fetchUser(withUid: uid) { user in <----- HERE I GOT AN ERROR Extra trailing closure passed in call
            self.currentUser = user
        }
    }
}

import Foundation
import UIKit
import FirebaseAuth
import SwiftUI

class ProfileViewController: UIViewController
{
    var authViewModel = AuthViewModel()
    @IBOutlet weak var userNameLabel: UILabel!

    override func viewDidLoad()
    {
        navigationItem.hidesBackButton = true
        userNameLabel.text = authViewModel.currentUser?.username // HERE I WANT TO DISPLAY CURRENT USER - USERNAME
        print(userNameLabel.text)
        
    }
}

I tried fill currentuser with user but nothings worked. Still in profile view controller I got nill. (currentUser.username = nil)

here is UserService Here is UserService class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM