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
?
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
.