61

I'm building an app with SwiftUI. When I was trying to display a sheet (previously Modal), this error message appears:

Thread 1: Fatal error: No observable object of type BixiStationCombinedListViewModel.Type found.

A View.environmentObject(_:) for BixiStationCombinedListViewModel.Type may be missing as an ancestor of this view.

This error occurs when I'm using a @State variable to display a modal that includes a Map View using MapKit.

I don't see why and how I should implement a new Environment Object.

Is it because the Station I select when tapping on the CardView should be stored globally and the info should be passed to the dedicated View?

The View handling the @State:

struct CardView: View {
    
    @EnvironmentObject var bixiModel: BixiStationCombinedListViewModel
    @State private var isModalOpen: Bool = false
        
    var station: BixiStationCombinedViewModel
        
    var body: some View {
        
        ZStack(alignment: .leading) {
            
                Card()
                
                StationTextInfo(station: station)

        } .onTapGesture {
            self.isModalOpen = true
            print(self.isModalOpen)
        }
        .sheet(isPresented: self.$isModalOpen) {
            BixiStationDetailView(station: self.station)
        }

    }
}

The View I'm trying to show within the sheet:

struct BixiStationDetailView: View {
    
    @EnvironmentObject var bixiModel: BixiStationCombinedListViewModel
        
    var station: BixiStationCombinedViewModel
    
    var body: some View {
        VStack {
            MapView(coordinate: station.coordinate, name: station.name)        
        }
    }
}

Finally the Object:

class BixiStationCombinedListViewModel: ObservableObject {
    
    init() {
        fetchDataFromApi()
    }
    
    @Published var stationsCombinedList = [BixiStationCombinedViewModel]()
    
    var stationsInformationList = [BixiStationInformationViewModel]()
    var stationsDataList = [BixiStationDataViewModel]()
        
    func fetchDataFromApi() {

        }        
    }
}

How can I get rid of the error message and display the proper View?

0

1 Answer 1

96

You have to pass your environment object to BixiStationDetailView, otherwise it won't have anything to bind to its @EnvironmentObject.

.sheet(isPresented: self.$isModalOpen) {
    BixiStationDetailView(station: self.station)
        .environmentObject(self.bixiModel)
}

Since you're presenting BixiStationDetailView as a sheet, it isn't a subview of your CardView and therefore doesn't inherit its @EnvironmentObject.

4
  • Thanks it's working perfectly! Previously I added the @EnvironmentObject directly in the view and thought it was enough... But it wasn't.
    – Daymo502
    Aug 21, 2019 at 15:43
  • 21
    I got the same error because i was using the @EnvironmentObject annotated object inside init(). The environment object is available only after successful initialisation. Nov 3, 2019 at 15:50
  • 3
    I just ran into this. I assume since the presented sheet is a new hierarchy it doesn't pass along the environment. Nov 26, 2019 at 23:23
  • 1
    I just encountered this issue with NavigationLink. Adding it to parent views wasn't enough, I had to add the modifier to the destination: argument of the nav link. Not very intuitive.
    – Big_Chair
    Oct 6, 2021 at 11:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.