title: No ObservableObject of type found
body: <p>I'm trying to quick access a list with favourite items from the master view with a modal sheet. The favourite objects are kept in an <code>EnvironmentObject</code> array. In the modal sheet there is a button, where you can basically add/remove the object from the favourites list. However, when you remove an item, the <code>EnvironmentObject</code> gets empty and the app crashes:</p>
  
  <blockquote>
    <p>Thread 1: Fatal error: No <code>ObservableObject</code> of type <code>FavouritesList</code> found. </p>
  </blockquote>
  
  <p>In the log it says:</p>
  
  <blockquote>
    <p>A <code>View.environmentObject(_:)</code> for <code>FavouritesList</code> may be missing as an ancestor of this view.: file</p>
  </blockquote>
  
  <p>How do I ensure it goes back to the <code>ContentView</code> naturally?</p>
  
  <pre><code>import SwiftUI
  
  struct ContentView: View {
      @EnvironmentObject var favouriteList: FavouritesList
      @State private var presentingSheet = false
  
      var body: some View {
          NavigationView {
              List {
                  NavigationLink(destination: JudgementsView()) {
                      Text("Judgements")
                  }
                  NavigationLink(destination: SecondaryView()) {
                      Text("Secondary acts")
                  }
                  ScrollView(.horizontal, showsIndicators: false) {
                      VStack {
                          if favouriteList.items.isEmpty {
                              Text("Nothing favoured")
                          } else {
                              ForEach(favouriteList.items, id: \.self) { id in
                                  VStack {
                                      HStack {
                                          ForEach(judgementsTAXraw.filter {
                                              $0.id == id
                                          }) { judgement in
                                              NavigationLink(destination: FileViewer(file: judgement.id)) {
                                                  Button(judgement.title) {
                                                      self.presentingSheet = true
  
                                                  }.sheet(isPresented: self.$presentingSheet) {
                                                      ModalSheet(file: judgement.CELEX)
  
                                                  }
                                              }
  
                                          }
                                      }
                                      HStack {
                                          ForEach(secondaryTAXraw.filter {
                                              $0.id == id
                                          }) { secondary in
                                              NavigationLink(destination: FileViewer(file: secondary.id)) {
                                                  Text(secondary.title).padding()
                                              }
  
                                          }
                                      }
  
                                  }
  
  
                              }
                          }
  
                      }
                  }
  
              }
              .navigationBarTitle(Text("Test"))
          }
      }
  }
  
  struct ModalSheet: View {
      var file: String
  
      @State private var showCopySheet = false
  
      @EnvironmentObject var favouriteList: FavouritesList
      var body: some View {
          NavigationView {
              Text("Modal").navigationBarItems(trailing:
                  Button(action: {
                      self.showCopySheet = true
                  }) {
                      Image(systemName: "doc.on.doc").frame(minWidth: 40)
                  }.actionSheet(isPresented: $showCopySheet) {
                      ActionSheet(title: Text("What do you want to do?"), buttons: [
                          .destructive(Text("favText"), action: {
                              if let index = self.favouriteList.items.firstIndex(of: self.file) {
  
                                  self.favouriteList.items.remove(at: index)
  
  
                              } else {
                                  self.favouriteList.items.append(self.file)
  
                              }
                          }),
                          .cancel()
                      ])
                  }.frame(minWidth: 50)
              )
          }
      }
  }
  </code></pre>