-1

I want to put the value I get from the completion handler into Text view as a string, but I get an error. Here is what I try to do:

    func final(name: String, completion: @escaping (_ message: String)-> Void){
        guard let uid = AuthViewModel.shared.userSession?.uid else {return}
        Firestore.firestore().collection("users").document(uid).collection("chats").document(name).collection("messages").whereField("read", isEqualTo: false).getDocuments { (snapshot, _) in
            guard let documents = snapshot?.documents.compactMap({ $0.documentID }) else {return}
            
            let unread = documents.count
            let unreadString = String(unread)
            completion(unreadString)
        }
    }

Try to put it in a text like so:

Text(model.final(name: name, completion: { (message) in
 String(message)
 }))

Here is the error I get

Type '()' cannot conform to 'StringProtocol'; only struct/enum/class types can conform to protocols
Raja Kishan
  • 16,767
  • 2
  • 26
  • 52
erdem
  • 41
  • 2

1 Answers1

-1

For simple solution use one message @State var. and update your view. Here is demo

struct ContentView: View {
    
    @State private var message: String = ""
    
    var body: some View {
        Text(message)
            .onAppear {
                model.final(name: "Name") { message in
                    self.message = message
                }
            }
    }
}
Raja Kishan
  • 16,767
  • 2
  • 26
  • 52