我是SwiftUi的新手。我正在尝试从Firebase实时数据库中读取数据并显示在列表中。
但是数据加载并保存到数组中,但是当我将其集成到列表中时。列表显示错误。
这是源代码
struct NotificationView: View {
@State private
var notificationArray = []
var body: some View {
// Here is the issue in list
List(self.notificationArray) {
noti in
Text(noti.title)
}.onAppear {
self.loadNotification()
}
}
}
func loadNotification() {
//For Admin
let rootRef = Database.database().reference().child("Admin").child("PushNotification")
rootRef.observeSingleEvent( of: .value, with: {
snapshot in
for rest in snapshot.children.allObjects as![DataSnapshot] {
guard
let restDict = rest.value as ? [String: Any]
else {
continue
}
let title = restDict["title"] as ? String
let description = restDict["description"] as ? String
self.notificationArray.append(PushNotification(title: title!, descripiton: description!))
}
})
}
}
struct NotificationView_Previews: PreviewProvider {
static
var previews: some View {
NotificationView()
}
}
struct PushNotification: Identifiable {
var id = UUID()
var title: String
var descripiton: String
}