Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

SwiftUI crashes because an EnvironmentObject may be missing as an ancestor

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

Short answer

A view that declares @EnvironmentObject needs an ancestor view to supply an object of that exact type with environmentObject(_:). Add the modifier above the view that crashes, usually at the app root. If your model uses the Observable macro, supply it with environment(_:) and read it with @Environment instead.

  • Applies to Xcode 26, SwiftUI iOS 26, Swift 6
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find the view that declares the property, then every place that view is created. The crash is in the path that has no environmentObject call above it.

    grep -rn "@EnvironmentObject" --include=*.swift .
    grep -rn "environmentObject(" --include=*.swift .
  2. 2Match what you see, then jump to its fix

    1. 1The crash happens when the first screen, or one specific screen, opensNo ancestor supplies the objectFix
    2. 2The app runs, but the Xcode canvas preview or a view created in another hierarchy crashesA preview or a separately built view has no objectFix
    3. 3The class is marked @Observable and has no ObservableObject conformanceThe model uses Observable, but the view still uses EnvironmentObjectFix

What does the error look like?

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

Cause 1 of 3

No ancestor supplies the object

1Check

The property wrapper only reads. You must set a matching object on an ancestor view with the environmentObject(_:) modifier, and the type must be the same as the declared type.

grep -rn "environmentObject(" --include=*.swift .

2Fix

Create the object once, at the root, and pass it down with the modifier.

@main
struct MyApp: App {
    @StateObject private var settings = Settings()
    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(settings)
        }
    }
}

3Verify

Run the app and open the screen that crashed.

xcodebuild build -scheme MyApp -destination 'generic/platform=iOS Simulator'

Confirmed by developer.apple.com

Did this fix it?

Cause 2 of 3

A preview or a separately built view has no object

1Check

Supplying the object applies to the view's hierarchy only. A preview builds a view on its own, so the root modifier does not reach it.

grep -rn "#Preview\|PreviewProvider" --include=*.swift .

2Fix

Add the same modifier to the view inside the preview, and to any view you create outside the main hierarchy.

#Preview {
    ProfileView()
        .environmentObject(Settings())
}

3Verify

Refresh the canvas; the preview renders.

# Xcode: Editor > Canvas > Resume (Option-Command-P)

Confirmed by developer.apple.com

Did this fix it?

Cause 3 of 3

The model uses Observable, but the view still uses EnvironmentObject

1Check

Apple's note says a model that conforms to Observable uses Environment, set with environment(_:) or environment(_:_:), not EnvironmentObject. Mixing the two leaves the lookup empty.

grep -rn "@Observable" --include=*.swift .

2Fix

Insert it with environment(_:) and read it with @Environment and the type.

// insert
ContentView().environment(library)

// read
@Environment(Library.self) private var library

3Verify

Build and run; the screen opens. If the object could be absent, declare the property optional so SwiftUI gives nil instead of throwing.

@Environment(Library.self) private var library: Library?

Confirmed by developer.apple.com

Did this fix it?

How do I stop it coming back?

  • Inject shared models once, at the App or root view, so every screen below has them.
  • Give every preview the same environment modifiers the real app has.
  • For new code on iOS 17 and later, prefer an Observable model with Environment.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Mobile apps

InstanceID token() no longer works in a Swift Firebase app

Instance ID calls fail to compile after upgrading Firebase to 8.0.0 or later

Firebase removed the Instance ID dependency from Messaging in 8.0.0, so InstanceID.instanceID() calls no longer belong in a current app.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.