BLACK FRIDAY SALE: Save 50% on all my Swift books and bundles! >>

Getting a Thread 1: Fatal error....NEED Help fixing

Forums > SwiftUI

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



I guess in preview?


@strych provides very few clues. Still, let's try to solve this!

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

Views are like LEGO kits. You can build great things from bits and pieces. Kits look great on their own. But you often combine several smaller kits into a larger kit for more stunning presentations. Sometimes, to build your kit, you may need another small bag of pieces from a different kit, an ancestor kit, if you will. Sometimes you can inject new pieces into your View kit, but most times, you'll just be injecting DATA from some other source.

I think this is the error message you are getting. You're asking SwiftUI to build a VIEW, probably with some fields, Text() objects, maybe a Slider() or a few Buttons(). But you're also asking the VIEW to include data from a kit named ClientStore and it seems to be an @Environment object.

But the compiler complains that it can't finish building this kit because it doesn't have the required parts, namely the ClientStore.


If you use your EnvironmentObject in the init of your View: This doesn't work and you must not do that.

Use a method and call the method in .onAppear.


Hacking with Swift is sponsored by Guardsquare

SPONSORED AppSweep by Guardsquare helps developers automate the mobile app security testing process with fast, free scans. By using AppSweep’s actionable recommendations, developers can improve the security posture of their apps in accordance with security standards like OWASP.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.