No ObservableOblect

Hi.
I'm studying SwiftUI, an error occurred when using .sheet in SwiftUI.
The code is:

Code Block
import SwiftUI
class processStatus: ObservableObject {
  @Published var isRun: Bool = false
}
struct Home: View {
  @EnvironmentObject var startCollect: processStatus
   
  var body: some View {
    ZStack {
      HomeBackgound()
      VStack {
        Button(action:{
          self.startCollect.isRun.toggle()
        }) {
          VStack {
            Text("あつめる")
            Text("Collect")
          }
        }
        .frame(width: 250, height: 250)
      }
      .sheet(isPresented: self.$startCollect.isRun) {
        SelectBall()
          .environmentObject(startCollect)
      }
    }
  }
}
struct Home_Previews: PreviewProvider {
  static var previews: some View {
    Home()
      .environmentObject(processStatus())
  }
}

The error is:
Code Block
Thread 1: Fatal error: No ObservableObject of type processStatus found. A View.environmentObject(_:) for processStatus may be missing as an ancestor of this view.

on line 23.

Can anyone give me some advice?
Thanks.

Addendum: Added error details and error locations.

an error occurred

You should better include whole error message and the line of the error, to get better responses sooner.

You should better include whole error message and the line of the error, to get better responses sooner.


Added error details and error locations. Sorry about that.

Added error details and error locations.

Thanks for adding the info.

But I cannot reproduce the error with your shown code.
You should better check other parts of your code using Home.
The code for the other files can be found here.
When you press each button, the screen transitions in the order of "Home", "SelectBall", and "Processing".

The code for the other files can be found here.

No parts using Home can be found. You may need to check somewhere else.
Home is only displayed in ContentView.swift. Here is the code.
Code Block
import SwiftUI
import Neumorphic
struct ContentView: View {
    var body: some View {
        Home()
    }
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Sorry if I can't provide the necessary files as I am a beginner. I would appreciate it if you could let me know if I have provided the wrong file.
Home needs to be passed the EnvironmentObject.
In ContentView you could add
Code Block Swift
@StateObject private var processStatus = processStatus()

and pass this into
Code Block Swift
Home()
.environmentObject(processStatus()) // add this

Accepted Answer

Sorry if I can't provide the necessary files

You have no need to be sorry, in many cases, it is hard to choose necessary files, unless you know the solution.
It is very nice you have shown it by request.
Thanks for showing the code.

Seems your problem is that you are not passing an instance of EnvironmentObject using .environmentObject modifier.
(Doing such in preview, does not work for the actual app.)

When you use @EnvironmentObject in a view, you need to instantiate the object and pass it to the view (or a ancestor of the view) using .environmentObject, as found in your Home_Previews.

Please try adding .environmentObject into your ContentView:
Code Block
struct ContentView: View {
var body: some View {
Home()
.environmentObject(processStatus())
}
}



In Swift, type names should start with Capital letter. Your processStatus looks a little bit odd.
(This is not critical, but you should better follow the most famous coding rule of Swift.)
It worked fine. Thank you!
As OOPer mentioned, the solution to the problem is to use .environmentObject(processStatus()) after calling a view. However, this solution only works when you are navigating from ContentView to other views using NavigationView. If you want to navigate to other views from your child views, and you use .environmentObject(processStatus()) for the target view, this method will not work, and the EnvironmentObject's properties will not be updated.
No ObservableOblect
 
 
Q