'How can I pass a variable into this function (in this instance)?

I am trying to display any post (firebase document) that contains a specific "postCreator" value. I have the query set up and working - the problem is trying to get them to actually show.

Just for clarification, if postCreator is "zbakaaa" then all documents with "zbakaaa" as its postCreator value in Firebase should show on the app.

When I run code like this, the app works properly - but only for that specific user ID. I am trying to get the code to run so that I can somehow set "userId" to post.postCreator, so when a user presses on the button to open this sheet, the post.postCreator is passed into the UserViewModel (class that following code is in) and then set to userId.

init() {
        getUserPosts()
    }
    
    func getUserPosts() {
        let userId = "rgOOOwD2cNdayJodbMgGGy9l9pB3"
        ref.collection("Posts").whereField("postCreator", isEqualTo: userId).addSnapshotListener { (snap, err) in...

I have tried something like this (which I have working in an unrelated application), but this just throws errors.

var post: PostModel? {
        didSet{
            if let post = self.post {
                getUserPosts(userId: post.postCreator)
            }
        }
    }

Here is my program code for reference:

PostModel

struct PostModel : Identifiable {
    
    var id: String
    var title: String
    var pic: String
    var likes: Int64
    var time: Date
    var likedBy: [String]
    var user: UserModel
    var postCreator: String
    
}

Button pressed to open this sheet (where postCreator should be put in?)

var post : PostModel
@State private var showingUserProfile = false
...

Button(action: {
                    showingUserProfile = true
                }){
                    WebImage(url: URL(string: post.user.pic)!)
                        .resizable()
                        .aspectRatio(contentMode: .fill)
                        .frame(width: 50, height: 50)
                        .clipShape(Circle())
                }   
                .transition(.move(edge: .leading))
                .sheet(isPresented: self.$showingUserProfile)
                {
                    UserView(post:post)
                }

UserViewModel Class

class UserViewModel : ObservableObject{
    
    @Published var posts : [PostModel] = []
    @Published var noPosts = false
    @Published var newPost = false
    @Published var updateId = ""
    
    let ref = Firestore.firestore()
    let uid = Auth.auth().currentUser!.uid
    
    init() {
        getUserPosts()
    }
    
    func getUserPosts() {
        let userId = "rgOOOwD2cNdayJodbMgGGy9l9pB3"
        ref.collection("Posts").whereField("postCreator", isEqualTo: userId).addSnapshotListener { (snap, err) in
            guard let docs = snap else{
                self.noPosts = true
                return
            }

...


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source