No ObservableObject

Studing SwiftUI in Apple's HandlingUserInput, I'm blocked in this error:
No ObservableObject of type UserData found. A View.environmentObject(_:) for UserData may be missing as an ancestor of this view.
The code is:

final class UserData: ObservableObject  {
    @Published var showFavoritesOnly = false
    @Published var landmarks = landmarkData
}
struct LandmarkList: View {
    @EnvironmentObject var userData: UserData

    var body: some View {
        NavigationView {
            List(landmarkData) { landmark in
               if !self.userData.showFavoritesOnly || landmark.isFavorite {
                    NavigationLink(destination: LandmarkDetail(landmark: landmark)) {
                        LandmarkRow(landmark: landmark)
                    }
               }
            }
            .navigationBarTitle(Text("Landmarks"))
        }
    }
}
struct LandmarkList_Previews: PreviewProvider {
    static var previews: some View {
            LandmarkList()
                 .environmentObject(UserData())
    }
}

".environmentObject(UserData())" is present in the preview.

"@EnvironmentObject var userData: UserData" in the View.

But the use of userData (if !self.userData.showFavoritesOnly) cause the fatal error.

Can someone help me in undertanding how to proceed?

Accepted Reply

Have you followed Step 4 - Section 5 of the tutorial?


> In

SceneDelegate.swift
, add the
environmentObject(_:)
modifier to the
LandmarkList
.


".environmentObject(UserData())" is present in the preview.


Preview is just used for previewing, you need to put `environmenObject(UserData())` somewhere in an actual app.

Replies

It is Landmark project: it is flawed as it is not updated for new versions of XCode

Search for Landmark in the forum, you will get a lot of threads and some solutions.


Note: you should move this thread to SwiftUI.

Have you followed Step 4 - Section 5 of the tutorial?


> In

SceneDelegate.swift
, add the
environmentObject(_:)
modifier to the
LandmarkList
.


".environmentObject(UserData())" is present in the preview.


Preview is just used for previewing, you need to put `environmenObject(UserData())` somewhere in an actual app.

Thank you Claude31and OOPer,

I did a mistake adding environmentObject into SceneDelegate.swift, revising it I saw the error.

  • Hello

    how do you manage this issue with Xcode 13.2.1 as SceneDelegate.swift doesn't appear anymore in the file list ?

    SwiftUI/EnvironmentObject.swift:70: Fatal error: No ObservableObject of type ToDoStorage found. A View.environmentObject(_:) for ToDoStorage may be missing as an ancestor of this view.

  • Hello

    how do you manage this issue with Xcode 13.2.1 as SceneDelegate.swift doesn't appear anymore in the file list ?

    SwiftUI/EnvironmentObject.swift:70: Fatal error: No ObservableObject of type ToDoStorage found. A View.environmentObject(_:) for ToDoStorage may be missing as an ancestor of this view.

Add a Comment

Hello

how do you manage this issue with Xcode 13.2.1 as SceneDelegate.swift doesn't appear anymore in the file list ?

SwiftUI/EnvironmentObject.swift:70: Fatal error: No ObservableObject of type ToDoStorage found. A View.environmentObject(_:) for ToDoStorage may be missing as an ancestor of this view.

You'd better start a new thread than ask new question in another (closed) one. And reference this older thread in your post.

Add the .environmentObject(UserData()) at the @main point in WindowGroup in the View

For eg: @main

struct YourApp: App {

    @StateObject var userData = UserData()

    

    var body: some Scene {

        

        WindowGroup

        {

            YourMainView().environmentObject(userdata)                 

        }

    }

}

Also Try replacing the if !self.userData.showFavoritesOnly with if userData.showFavouritesOnly != false or true // According to your condition