简体   繁体   English

类型 'Void' 不能符合 'View' | Swift

[英]Type 'Void' cannot conform to 'View' | Swift

I'm trying to create a List with data from my firebase reali-time database but i'm getting this error on the List line:我正在尝试使用我的 firebase 实时数据库中的数据创建一个列表,但我在列表行中收到此错误:

The error:错误:

Type 'Void' cannot conform to 'View' 

My code: struct ActiveGoalsView: View {我的代码: struct ActiveGoalsView: View {

    @State var goals = ["finish this project"]
    @State var ref = Database.database().reference()
    
    var body: some View {
        NavigationView {
            List {
                
                ref.child("users").child(Auth.auth().currentUser?.uid ?? "noid").child("goals").observeSingleEvent(of: .value) { snapshot in
                    
                    for snap in snapshot.children {
                        Text(snap.child("title").value)
                    }
                }
                
            }.navigationBarHidden(true)
        }
    }
}

struct ActiveGoalsView_Previews: PreviewProvider {
    static var previews: some View {
        ActiveGoalsView()
    }
}

You can't use imperative code like observeSingleEvent in the middle of your view hierarchy that doesn't return a View .您不能在不返回View的视图层次结构中间使用像observeSingleEvent这样的命令式代码。 As a commenter suggested, you'd be better off moving your asynchronous code outside of the body (I'd recommend to an ObservableObject ).正如评论者所建议的那样,您最好将异步代码移到body之外(我建议使用ObservableObject )。 Here's one solution (see inline comments):这是一种解决方案(请参阅内联评论):

class ActiveGoalsViewModel : ObservableObject {
    @Published var children : [String] = []
    
    private var ref = Database.database().reference()

    func getChildren() {
        ref.child("users").child(Auth.auth().currentUser?.uid ?? "noid").child("goals").observeSingleEvent(of: .value) { snapshot in
            
            self.children = snapshot.children.map { snap in
                snap.child("title").value //you may need to use ?? "" if this returns an optional
            }
        }
    }
}

struct ActiveGoalsView: View {
    @State var goals = ["finish this project"]
    @StateObject private var viewModel = ActiveGoalsViewModel()

    var body: some View {
        NavigationView {
            List {
                ForEach(viewModel.children, id: \.self) { child in //id: \.self isn't a great solution here -- you'd be better off returning an `Identifiable` object, but I have no knowledge of your data structure
                    Text(child)
                }
            }.navigationBarHidden(true)
        }.onAppear {
            viewModel.getChildren()
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 '(snap: DataSnapshot) =&gt; void' 类型的参数不可分配给 '(a: DataSnapshot) =&gt; boolean' 类型的参数 - Argument of type '(snap: DataSnapshot) => void' is not assignable to parameter of type '(a: DataSnapshot) => boolean' 尝试在 swift 应用程序的 Firestore 中添加数组元素时出现“无法将 'String' 类型的值转换为预期的参数类型 '[Any]'”错误 - "Cannot convert value of type 'String' to expected argument type '[Any]'" error when trying to add array element in firestore in swift app 尝试从 Firebase 实时数据库读取数据时出现错误“Static 方法‘buildBlock’要求‘UInt’符合‘View’” - I am getting error" Static method 'buildBlock' requires that 'UInt' conform to 'View'" when trying to read data from Firebase Realtime Database 在 scope 中找不到类型 GIDSignInDelegate - Cannot find type GIDSignInDelegate in scope Firestore 和 Swift UI 表达式类型不明确 - Firestore and Swift UI Type of expression is ambiguous 错误:参数类型 'Future<dynamic> ' 不能分配给参数类型 'void Function()?'</dynamic> - Error: The argument type 'Future<dynamic>' can't be assigned to the parameter type 'void Function()?' 无法从 Swift 中的 Firestore 获取文档 - Cannot get documents from Firestore in Swift 无法将快照数据转换为事件类型 - Cannot cast Snapshot Data as type Event 在 AWSSDKS3 中找不到 EndpointResolver 类型的处理程序 - Cannot find a handler of type EndpointResolver in AWSSDKS3 无法将类型“[String: Any]”的返回表达式转换为返回类型“Song?” - Cannot convert return expression of type '[String : Any]' to return type 'Song?'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM