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
Attempt to present toast whose view is not in the window hierarchy #19
Comments
It seems to me that you're creating a dummy hidden button just to attach the |
Yep, that's correct. The gist of the situation is that this toast is for a View where a user can upload an image to Firebase Storage, however before the toast is presented it verifies that the photo is correctly formatted. If it isn't, then it shows an alert (which I've also set to be programmatically triggered in almost the exact same way, tying it to a dummy hidden button. It goes something like this: @State private var presentingToast = false
@State private var uploadProgress = 0.0
var body: some View {
VStack {
Button(action: {
print("Show toast")
}, label: {
Text("")
})
.toast(isPresented: $presentingToast) {
ToastView("Uploading...")
.toastViewStyle(DefiniteProgressToastViewStyle(value: $uploadProgress))
}
.hidden()
...
}
//Function gets called after an ImagePicker is dismissed
func loadImage() {
guard let inputImage = inputImage else { return } //This is pulled from an ImagePicker sheet set up elsewhere
local data = inputImage.pngData()!
//Do other checks on the image...
...
//If all checks are OK
presentingToast = true //In theory, the toast should present itself here, but the aforementioned error happens here (when I step to this line in the debugger, this is where the error pops itself up)
...
} |
How did you implement the callback after the user has picked an image? Are you calling your |
It's tied to the onDismiss parameter of the sheet modifier @State private var showingImagePicker = false
@State private var inputImage: UIImage?
...
var body: some View {
VStack(alignment: .leading) {
...
Button(action: {
showingImagePicker = true
}, label: {
Text("Upload photo")
Image(systemName: "chevron.right")
})
...
}
.sheet(isPresented: $showingImagePicker, onDismiss: loadImage) {
ImagePicker(image: self.$inputImage)
}
}
func loadImage() {
...
} |
Try calling your function using .sheet(isPresented: $showingImagePicker) {
ImagePicker(image: $inputImage)
.onDisappear {
loadImage()
}
} Would you mind showing me how do you implement the |
Ah, yep, that did the trick. Thanks so much! As for the ImagePicker, it's pretty much exactly as you said. I have a |
By calling |
Pre-requisites:
When attempting to trigger a toast to appear programmatically, I get the following error:
The relevant section of my code looks like this:
When I set presentingToast to true, it gives me the aforementioned error. I've also tried removing the hidden modifier and putting in some words in the Text, but that doesn't make a difference.
Your Environment
The text was updated successfully, but these errors were encountered: