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

I have simple app, when I run my project it work, but then throw error after running like "Thread 1: Fatal error: No ObservableObject of type msgDatas found. A View.environmentObject(_:) for msgDatas may be missing as an ancestor of this view."

Code Block struct Home: View {
  @EnvironmentObject var data : msgDatas
   
  var body : some View{
     
    ZStack{
       
      Color("bg").edgesIgnoringSafeArea(.top)
       
      NavigationLink(destination: Message(), isActive: $data.show) {
       
        Text("")
      }
      VStack{
         
        topView()
      }
    }
  }
}


Answered by skysoft13 in 667892022
I solve problem, when I calling @State

Code Block struct Home: View {
  @EnvironmentObject var data : msgDatas
  @State var show: Bool = false
 
   
  var body : some View{
     
    ZStack{
       
      Color("bg").edgesIgnoringSafeArea(.top)
       
      NavigationLink(destination: Chat(), isActive: self.$show) {
       
        Text("")
      }
      VStack{
         
        topView()
      }
    }
  }
}


Isn't the error message clear enough?
Where is the code calling environmentObject?
Accepted Answer
I solve problem, when I calling @State

Code Block struct Home: View {
  @EnvironmentObject var data : msgDatas
  @State var show: Bool = false
 
   
  var body : some View{
     
    ZStack{
       
      Color("bg").edgesIgnoringSafeArea(.top)
       
      NavigationLink(destination: Chat(), isActive: self.$show) {
       
        Text("")
      }
      VStack{
         
        topView()
      }
    }
  }
}


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