试图在概要视图SwiftUI上显示获取的firebase数据

试图在概要视图SwiftUI上显示获取的firebase数据,firebase,firebase-realtime-database,swiftui,Firebase,Firebase Realtime Database,Swiftui,我正在尝试显示从Firebase数据库获取的数据。我尝试创建@Statevar变量并将它们添加到我的函数中,但没有成功。我试着在一个按钮中打印我的函数输出,并将其打印到控制台上,结果成功了。我只是不知道如何在我的代码视图中显示它们 import SwiftUI import Firebase struct ProfileView: View { var body: some View { VStack { Button(actio

我正在尝试显示从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)
        }    
    }    
}

只需创建一个@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的示例,因此无法测试它。如果它不起作用,请描述其行为。

点击
按钮后,添加
@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”感谢您的回复!但是我使用了未解析标识符“self”时出现了此错误。此外,在我用于在视图之间导航的选项卡视图中,我在添加@State VariableMake后立即在调用中获得了缺少的参数。请确保函数位于结构内部。如果您添加@State vatname:String=“”使用默认值,您不应该在其他视图中获得错误