SwiftUI View and @FetchRequest predicate with variable that can change : <p>I have a view showing messages in a team that are filtered using @Fetchrequest with a fixed predicate 'Developers'.</p>

<pre><code>struct ChatView: View {

@FetchRequest(
    sortDescriptors: [NSSortDescriptor(keyPath: \Message.createdAt, ascending: true)],
    predicate: NSPredicate(format: "team.name == %@", "Developers"),
    animation: .default) var messages: FetchedResults&lt;Message&gt;

@Environment(\.managedObjectContext)
var viewContext

var body: some View {
    VStack {
        List {
            ForEach(messages, id: \.self) { message in
                VStack(alignment: .leading, spacing: 0) {
                    Text(message.text ?? "Message text Error")
                    Text("Team \(message.team?.name ?? "Team Name Error")").font(.footnote)
                }
            }...
</code></pre>

<p>I want to make this predicate dynamic so that when the user switches team the messages of that team are shown. The code below gives me the following error </p>

<blockquote>
  <p>Cannot use instance member 'teamName' within property initializer; property initializers run before 'self' is available</p>
</blockquote>

<pre><code>struct ChatView: View {

@Binding var teamName: String

@FetchRequest(
    sortDescriptors: [NSSortDescriptor(keyPath: \Message.createdAt, ascending: true)],
    predicate: NSPredicate(format: "team.name == %@", teamName),
    animation: .default) var messages: FetchedResults&lt;Message&gt;

@Environment(\.managedObjectContext)
var viewContext

...
</code></pre>

<p>I can use some help with this, so far I'm not able to figure this out on my own.</p>
