0

How do I pass the child view to the ancestor view

ERROR ON ForEach = Thread 1: Fatal error: No ObservableObject of type MenuListFriendshipModel found. A View.environmentObject(_:) for MenuListFriendshipModel may be missing as an ancestor of this view.

ERROR ON List = Thread 1: Fatal error: No ObservableObject of type MenuListFriendshipModel found. A View.environmentObject(_:) for MenuListFriendshipModel may be missing as an ancestor of this view.

// ListView.swift

import SwiftUI

struct FriendshipListView: View {
    
    @EnvironmentObject var listViewModel: MenuListFriendshipModel
    
    var body: some View {
        List {
            //Use the $ syntax to pass a binding on to the subviews
            ForEach($listViewModel.items) { $item in
                MenuListRowView(item: $item)
            }
        }
        .navigationTitle("Friendships / Relationships")
        .listStyle(PlainListStyle())
    }
}


struct ListView_Previews: PreviewProvider {
    static var previews: some View {
        FriendshipListView()
        .environmentObject(MenuListFriendshipModel())
    }
}

ERROR ON = Thread 1: Fatal error: No ObservableObject of type MenuListFriendshipModel found. A View.environmentObject(_:) for MenuListFriendshipModel may be missing as an ancestor of this view.

//  ManifestationMenuApp.swift

import SwiftUI

@main
struct ManifestationMenuApp: App {
    
    @AppStorage("isOnboarding") var isOnboarding: Bool = true
    
    var body: some Scene {
        WindowGroup {
            if isOnboarding {
                OnboardingView()
            } else {
                MainView()
            }
        }
    }
}

  • 1
    You need to do `.environmentObject(MenuListFriendshipModel())` in your actual `App`, as you did in your `Preview` code. Doesn't have anything to do with a "child" or "ancestor" view – jnpdx Oct 03 '22 at 01:18
  • have a look at this info: https://developer.apple.com/documentation/swiftui/managing-model-data-in-your-app it shows you how to use `ObservableObject` – workingdog support Ukraine Oct 03 '22 at 01:37

0 Answers0