Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fatal error crash when accessing environment object on initial render inside of WrappingHStack #47

Open
swestwood opened this issue Aug 7, 2023 · 3 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@swestwood
Copy link

Describe the bug
When a view nested inside of WrappingHStack tries to access an environment object on its initial render, it cannot find it and throws a fatal error. (see example below)

If the nested view waits until after the first render to access the environment object, it works.

In either case for the code below, replacing WrappingHStack with HStack works properly.

To Reproduce
Steps to reproduce the behavior:

  1. Set an environment object outside of the WrappingHStack
  2. Try to access the environment object in a subview of WrappingHStack
  3. Fatal error about the environment object not existing

Expected behavior
Replacing WrappingHStack with HStack gives the expected behavior of being able to access the environment object on initial render instead of crashing.

Context:

  • WrappingHStack version: 2.2.9
  • Model: Simulator iPhone 14 Pro

Additional context
Add any other context about the problem here.

Simplified Example
(adapted from https://www.hackingwithswift.com/quick-start/swiftui/how-to-use-environmentobject-to-share-data-between-views)

class GameSettings: ObservableObject {
    @Published var score = 0
}

struct ScoreView: View {
    @EnvironmentObject var settings: GameSettings
    @State var ready = true  // Set this to true and the code crashes, set it to false to skip the initial render, and it works.

    var body: some View {
        Text("Score: \(ready ? settings.score : -1)")
            .onAppear {
                ready = true
            }
    }
}
struct WrappingHStackBugDemo: View {
    @StateObject var settings = GameSettings()

    var body: some View {
        VStack {
            Button("Increase Score") {
                settings.score += 1
            }
            // Replacing this with a regular HStack works in all cases, including initial render
            WrappingHStack {
                ScoreView()
            }
        }
        .frame(height: 200)
        .environmentObject(settings)
    }
}


struct WrappingHStackBugPreview: PreviewProvider {
    static var previews: some View {
        WrappingHStackBugDemo()
    }
}
@swestwood swestwood added bug Something isn't working help wanted Extra attention is needed labels Aug 7, 2023
@wscqs
Copy link

wscqs commented Mar 21, 2024

I also encountered it, how to solve it

@dkk
Copy link
Owner

dkk commented Mar 22, 2024

Probably related to #15

A relatively easy workaround would be to pass the environmentObjects as parameters to you views

Example:

Instead of

WrappingHStack{
   YourView()
}
.environment(someObject)

do:

WrappingHStack{
   YourView(someObject)
}

Repository owner deleted a comment from wscqs Mar 22, 2024
@DanielSmith1239
Copy link

DanielSmith1239 commented Nov 21, 2024

I'm having this issue as well. Is there a way to fix this without removing my environment object?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants