How to setup SwiftData in a Widget

So I have been trying to add a widget to my app. I am trying to add SwiftData to sync data from my app to the widget. My attempt at doing that is shown below:

import SwiftData
import SwiftUI
import WidgetKit

@main
struct CubeSpeedWidgetBundle: WidgetBundle {
    let fullSchema = Schema([Time.self, Session.self])
    let configuration = ModelConfiguration(schema: Schema([Time.self, Session.self]), inMemory: false, readOnly: false, cloudKitDatabase: .automatic)
    var body: some Widget {
        CubeSpeedWidget()
            .modelContainer(try! ModelContainer(for: fullSchema, configurations: [configuration]))
        CubeSpeedWidgetLiveActivity()
    }
}

Xcode says that "Value of type 'CubeSpeedWidget' has no member 'modelContainer'". How should I do this properly?

Thanks.

Accepted Answer

I haven't looked at this, but it seems to me that all the SwiftData stuff should be within the widget itself, not the WidgetBundle? I think View has a .modelContainer member?

How to setup SwiftData in a Widget
 
 
Q