I am trying to set string companyName with a value fetched from firebase. It takes too long to use .onAppear as the view loads without the company name and then after a few milliseconds it appears. I tried to use init() but I'm getting A error
Escaping closure captures mutating 'self' parameter.
How can I go round this as I need the value of companyName to be fetched before the view?
struct Home: View {
@AppStorage("currentPage") var currentPage = 1
@State public var companyName: String = ""
var db = Firestore.firestore()
init() {
let user = Auth.auth().currentUser
let userName = user?.email ?? ""
let docRef = db.collection("CONTACT").document(userName)
var buffer = ""
docRef.getDocument { (document, error) in
if let document = document, document.exists {
//Setting Values
let data = document.data()
_companyName = State(initialValue: data?["companyName"] as? String ?? "")
} else {
print("Document does not exist")
}
}
}