Filter a list of Item objects on a related Product Object passed as a Bindable

I'm currently developing a SwiftUI application that utilizes SwiftData for data management. I am facing a challenge when trying to filter a query. Specifically, I want to filter a list of Item objects to match a Product instance that is passed to my View.

error : Instance member 'product' cannot be used on type 'MainItemListEncap'; did you mean to use a value of this type instead

The view

//
//  311.1.1. MainRefToItem.swift
//  ComparePrice
//
//  Created by Herman VAN CAUWELAERT on 11/12/2024.
//

import SwiftUI
import SwiftData

struct MainItemListEncap: View {
    @Bindable var product: Product
    

    @Query(
        filter: #Predicate { item in
            item.productGroup == product
        },
        sort: [SortDescriptor(\Item.name)]
    )
    var items: [Item]
    
    @Environment(\.modelContext) var modelContext
   
    var body: some View {
        ForEach(items) { item in
            
            VStack(alignment: .leading) {
                    Text(item.name)
                    Text(item.description)
                }
            
           
        }
    }
}

the product class.


import SwiftData
import Foundation

@Model
final class Product: CustomStringConvertible, CustomDebugStringConvertible  {
    @Attribute(.unique) var productName: String
    var productDescription: String
    var outputCurrency: String
    
    // Gebruik een `String` als opslag voor `outputSystem`
    var outputSystemRawValue: String = MeasurementSystem.metric.rawValue
    
    // Computed property om `MeasurementSystem` te gebruiken
    var outputSystem: MeasurementSystem {
        get {
            MeasurementSystem(rawValue: outputSystemRawValue)
        }
        set {
            outputSystemRawValue = newValue.rawValue
        }
    }
    
    // er zijn verschillend item versies voor een product
    // als er een hoofdproduct gedelete wordt, dan zullen alle onderliggende items ook gedelete worden
    
    @Relationship(deleteRule: .cascade, inverse: \Item.productGroup) var refToItems = [Item]()

    init(productName: String = "", productDescription: String = "what is this product", outputCurrency: String = "EUR", outputSystem: MeasurementSystem = MeasurementSystem.metric) {
            self.productName = productName
            self.productDescription = productDescription
            self.outputCurrency = outputCurrency
            self.outputSystem = outputSystem
        }
    }

the item class


import Foundation
import SwiftData

@Model
final class Item: CustomStringConvertible, CustomDebugStringConvertible {
    var timestamp: Date

    @Attribute(.unique) var name: String

    var productGroup: Product?
    var shop: String //TODO: becomes Shop
    
    var price: Double
    var currency: String
    var quantity: Double
    var unit: String //TODO: becomes Unit
    var isShown : Bool
    var minimumQuantity: String
    
    var rateStr: String {
        conversionRate != nil ? " (rate: \(ValueFormatter.shared.format(conversionRate!)))" : ""
        }
    

    init(timestamp: Date = Date()
         , name: String
         , shop: String = "Colruyt"
         , price:Double = 1.00
         , currency: String = "EUR"
         , quantity: Double = 1.0
         , unit: String = "g"
         , isShown:Bool = true
         , conversionRate: Decimal? = 1
         , minimumQuantity: String = "1"
    ) {
        
        self.timestamp = timestamp
        self.name = name
        self.shop = shop
        self.price = price
        self.currency = currency
        self.quantity = quantity
        self.unit = unit
        self.isShown = isShown
        self.conversionRate = conversionRate
        self.minimumQuantity = minimumQuantity
    }
    }
Answered by RFIHW in 819190022

yes, you can only use static or let value in Query predicate at declaration.

the post can be removed

Accepted Answer

yes, you can only use static or let value in Query predicate at declaration.

Filter a list of Item objects on a related Product Object passed as a Bindable
 
 
Q