Post

Replies

Boosts

Views

Activity

Reply to SwiftData Fatal error: failed to find a currently active container
I found that it works in a simulator, but not on an iPhone or the preview(might be a beta bug). Here is my code: import SwiftUI import SwiftData struct DiscoverView: View { @Environment(\.modelContext) private var context @Query private var rootBeers: [RootBeerData] var body: some View{ NavigationStack{ List(rootBeers) { rootbeer in discoverSumary(title: rootbeer.name, desc: rootbeer.desc, icon: rootbeer.icon) } .navigationTitle("Discover") .listStyle(.inset) } } } #Preview { DiscoverView() } And this is the data model: import Foundation import SwiftData import SwiftUI @Model class RootBeerData{ var name: String var desc: String var icon: String init(name: String, desc: String, icon: String) { self.name = name self.desc = desc self.icon = icon } } And don't forget to add the .modelContainer(for: RootBeerData.self) modifier to ContentView()
Jul ’23