In my userlistview I attached the user model to search users in the search bar from Firebase but my search bar is showing up as empty I am not able to view or search for the users in the search bar from firebase so I set a breakpoint and I received this error.
self.users = documents.compactMap({ try? $0.data(as: User.self)})
Argument passed to call that takes no arguments
Cannot convert value of type () to closure result type User?
How do I fix this code and how can I properly rewrite this code to be able to search for my users that is saved in firebase ?
import FirebaseFirestore
import FirebaseFirestoreSwift
import Firebase
class UserViewModel: ObservableObject{
@Published var users = [User]()
init() {
fetchUsers()
}
func fetchUsers(){
Collection_Users.getDocuments{ snapshot, _ in
guard let documents = snapshot?.documents else{return}
self.users = documents.compactMap({ try? $0.data(as: User.self)})
print(self.users)
}
}
func filteredUsers(_ query: String ) -> [User] {
let loweredCase = query.lowercased()
return users.filter({$0.username.contains(loweredCase)})
}
}
import SwiftUI
struct UserList: View {
@ObservedObject var viewModel: UserViewModel
@Binding var searchText: String
var users: [User] {
return searchText.isEmpty ? viewModel.users : viewModel.filteredUsers(searchText)
}
var body: some View {
ScrollView {
LazyVStack {
ForEach(users) { user in
NavigationLink(
destination:ProfileHead(user: user),
label: {
UserCell(user: user)
.padding(.leading)
})
}
}
}
}
}
struct Search: View {
@State var searchText = ""
@State var inSearchMode = false
@ObservedObject var newModel = UserViewModel()
var body: some View {
ScrollView{
SearchBar(text: $searchText, isEditing: $inSearchMode)
.padding()
ZStack{
if inSearchMode {
UserList(viewModel: newModel, searchText: $searchText)
}else{
GridView()
}