【问题标题】:Trying to show fetched firebase data on profile view SwiftUI试图在个人资料视图SwiftUI上显示获取的firebase数据
【发布时间】: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


    【解决方案1】:

    只需创建一个包含名称的@State 变量。如果您的函数更改了该变量,您的视图将更新。

    结构配置文件:查看 {

    @State var name : String = ""
    
    var body: some View {
    
        Text("Hello " + self.name)
    

    然后在你的函数中,而不是打印你将它分配给你的状态。

    let name = value?["fullName"] as? String ?? ""
    print(name)
    self.name = name
    

    应该可以。我目前没有使用 Firebase 的示例,因此无法对其进行测试。如果它不起作用,请描述行为。

    【讨论】:

    • 感谢您的回复!但是我收到此错误 Use of unresolved identifier 'self'。同样在我用于在视图之间导航的选项卡视图中,我在添加 @State 变量后立即调用 Missing 参数
    • 确保函数在结构体内部。如果您使用默认值添加@State vat name : String = ””,则不应在其他视图中出现错误
    【解决方案2】:

    点击Button后,添加@State属性profileName并在网络请求函数中分配即可。

    //  ProfileView.swift
    //
    //
    //  Created by Shahin Bararesh on 2020-09-07.
    //
    
    import SwiftUI
    import Firebase
    
    struct ProfileView: View {
    
        @State var profileName: String = ""
    
        var body: some View {
    
            VStack {
                Button(action: {
                    profilef()
                }) {
                    Text(profileName)
                }
    
                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 ?? ""
    
                self.profileName = name
    
                // ...
            }) { error in
                print(error.localizedDescription)
            }
    
        }
    
    }
    

    【讨论】:

    • 感谢您的回复。我得到这个使用未解析的标识符'self'
    猜你喜欢
    • 2020-09-21
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 1970-01-01
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多