0

I have a model with data string of name and a bool of UE. I'm trying to display item.name whenever UE is true. My issue is when whenever I run the code the data doesn't seem to read the UE. I got an error displaying the item.UE as a text view. The data that I am getting it from is from a database the item.name works without the conditional.

struct AttendingUsersView: View {
    
    @ObservedObject var model = UserViewModel()
    
    
    var body: some View {
        VStack {
            List (model.list) { item in
                if item.UE == true {
                    Text(item.name)
                } else {
                    Text("This isnt working")
                }
            }

            
            DismissButton
        }
    }

I've tried displaying the item.UE to see what it would display but I get an error saying "No exact matches in call to initializer".

UserViewModel file

class UserViewModel: ObservableObject {
    
    @Published var list = [Username]()
    
    
    func addData(name: String, UE: Bool) {
        
        //get a reference to the database
        let db = Firestore.firestore()
        
        
        // Add a new document in collection "username"
        db.collection("usernames").document(UserDefaults.standard.object(forKey: "value") as! String).setData([
                // MARK: Change the parameters to the users inputed choices
                "name": name,
                "UE": UE
            ]) { err in
                if let err = err {
                    print("Error writing document: \(err)")
                } else {
                    print("Document successfully written!")
                }
            }
    }
    
    
    func getData() {
        
        //get a reference to the database
        let db = Firestore.firestore()
        
        //Read the documents at a specific path
        db.collection("usernames").getDocuments { snapshot, error in
            
            //checking for errors
            if error == nil {
                //no errors
                
                if let snapshot = snapshot {
                    // update
                    DispatchQueue.main.async {
                        
                        // Get all the documents and create usernames
                        self.list = snapshot.documents.map { d in
                            
                            //Create a Username
                            return Username(id: d.documentID, name: d["name"] as? String ?? "", UE: (d["UE"] != nil)) //cast as a string and if not found return as a empty string 
                        }
                    }
                }
            } else {
                //Handle the error
                
            }
        }
    }
    
    
    
    
    
}

Username model

struct Username: Identifiable {
    
    var id: String
    var name: String
    var ue: Bool
    
}
7
  • Without showing the code for whatever type item is, this isn't possible to debug Commented Nov 18, 2022 at 21:27
  • where would i find that? wouldn't item be whatever the iteration be for the model.list inside the body? Commented Nov 18, 2022 at 21:36
  • We don't see any code for UserViewModel, so I don't know what type item is. I don't know where to find that in your code. Commented Nov 18, 2022 at 21:42
  • I added the code for UserViewModel thanks for the input. Commented Nov 18, 2022 at 21:49
  • You didn't include Username, which is apparently the type of item, but it does clear some things up. What do you mean by "it doesn't read the UE? Commented Nov 18, 2022 at 21:53

1 Answer 1

1

Try this, with fixes for your ue in your getData, and in the view display.

struct ContentView: View {
    var body: some View {
        AttendingUsersView()
    }
}

struct AttendingUsersView: View {
    @StateObject var model = UserViewModel()  // <-- here
    
    var body: some View {
        VStack {
            List (model.list) { item in
                if item.ue {  // <-- here
                    Text(item.name)
                } else {
                    Text("This is working ue is false")
                }
            }
           // DismissButton
        }
    }
}

class UserViewModel: ObservableObject {
    // for testing
    @Published var list:[Username] = [Username(id: "1", name: "item-1", ue: false),
                                    Username(id: "2", name: "item-2", ue: true),
                                    Username(id: "3", name: "item-3", ue: false)]
    
    func addData(name: String, UE: Bool) {
        //get a reference to the database
        let db = Firestore.firestore()
        // Add a new document in collection "username"
        db.collection("usernames").document(UserDefaults.standard.object(forKey: "value") as! String).setData([
            // MARK: Change the parameters to the users inputed choices
            "name": name,
            "UE": UE
        ]) { err in
            if let err = err {
                print("Error writing document: \(err)")
            } else {
                print("Document successfully written!")
            }
        }
    }
    
    func getData() {
        //get a reference to the database
        let db = Firestore.firestore()
        //Read the documents at a specific path
        db.collection("usernames").getDocuments { snapshot, error in
            //checking for errors
            if error == nil {
                //no errors
                if let snapshot = snapshot {
                    // update
                    DispatchQueue.main.async {
                        // Get all the documents and create usernames
                        self.list = snapshot.documents.map { d in
                            //Create a Username
                            // -- here, ue:
                            return Username(id: d.documentID, name: d["name"] as? String ?? "", ue: (d["UE"] != nil)) // <-- here ue:
                        }
                    }
                }
            } else {
                //Handle the error
            }
        }
    }
}

struct Username: Identifiable {
    var id: String
    var name: String
    var ue: Bool
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.