onAppear is too late to set state. You should have the state configured first, then body is designed to create all the Views depending on the state. This is a crucial part of SwiftUI's design where "views are a function of state".
onAppear is designed for external actions unrelated to state.
Since you are using SwiftData it's not @State it is @Query to fetch the models and then bind the Views directly to them. If the types are different you can use computed bindings to convert.
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: