App says I need to Set a .modelContext in view's environment to use Query

Only I do:

        .sheet(isPresented: self.$showMoveItemSheet) {
            MoveItemsView(items: Array(self.selectedItems), from: self.room)
                .modelContext(self.context)
        }
        .modelContext(self.context)

and then in the MoveItemsView I have

    @Environment(\.modelContext) var context

<pause> Hm, I'm setting the Query programmatically during init, would that be the cause? 'cause it does seem to work...

I am also facing the same issue in a Sheet. Did you find any solution?

I was getting this same error. As the OP mentioned, this error is from using a query inside a view's init(){}. In my case, I just wanted to get the query count (for debugging purposes) and was attempting to access the value within the view 's init(){}.

I moved my print statement to the .onAppear() for my view and everything works great!

App says I need to Set a .modelContext in view's environment to use Query
 
 
Q