Migrating from the Observable Object protocol to the Observable macro

// Before I was able to do something like:

struct ContentView: View {
    @EnvironmentObject var listData: ListData
    
    var body: some View {
        ListView($listData.listDataArray)
    }
}
struct ListView: View {
    @Binding var listDataArray: [DataType]
}

class ListData: ObservableObject {
    @Published var listDataArray: [DataType] = []
}

// Now, I will get the error "Cannot find '$listData' in scope" on the line indicated below when I try to migrate to the Observable macro

struct ContentView: View {
    @Environment(ListData.self) var listData
    
    var body: some View {
        ListView($listData.listDataArray)  ----------> Error 
    }
}
struct ListView: View {
    @Binding var listDataArray: [DataType]
}

@Observable
class ListData {
    var listDataArray: [DataType] = []
}

@main
struct SomeApp: App {
    @State private var listData = ListData()
    
    var body: some Scene {
        WindowGroup {
            ContentView
                .environment(listData)
        }
    }
}
Accepted Answer

I was able to fix this by creating a @Bindable in the view body:

struct ContentView: View {
    @Environment(ListData.self) var listData
    
    var body: some View {
        // Add this line
        @Bindable var listData = listData

        ListView($listData.listDataArray)
    }
}

I think you want to change @Binding to @Bindable

from:

struct ListView: View {
    @Binding var listDataArray: [DataType]
}

to:

struct ListView: View {
 @Bindable var listDataArray: [DataType]
}

I worry that you might have unexpected consequences to add the @Bindable in the body

Did you test just passing in ListView(Bindable(listData).listDataArray), instead of creating a @Bindable in the View body? I think it should work.

Migrating from the Observable Object protocol to the Observable macro
 
 
Q