【发布时间】:2020-09-12 20:31:12
【问题描述】:
我正在尝试显示从 Firebase 数据库中获取的数据。我尝试创建 @State var 变量并将它们添加到我的函数中,但它不起作用。我尝试在按钮中打印我的函数输出以将其打印到控制台并且它可以工作。我只是不知道如何在我的代码视图中显示它们
import SwiftUI
import Firebase
struct ProfileView: View {
var body: some View {
VStack {
Button(action: {
profilef()
}) {
Text("hello")
}
HStack {
Button(action: {
try! Auth.auth().signOut()
UserDefaults.standard.set(false, forKey: "status")
NotificationCenter.default.post(name: NSNotification.Name("statusChange"), object: nil)
}) {
Text("Logout")
}
}
}
}
func profilef() {
let userID = Auth.auth().currentUser?.uid
let ref = Database.database().reference()
ref.child("UserInfo").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
// Get user value
let value = snapshot.value as? [String : AnyObject]
let name = value?["fullName"] as? String ?? ""
print(name)
// ...
}) { error in
print(error.localizedDescription)
}
}
}
【问题讨论】:
标签: firebase firebase-realtime-database swiftui