Sheet content has stale `@State` values

Originator:darren.mo
Number:rdar://FB13455221 Date Originated:2023-12-07
Status:Open Resolved:
Product:SwiftUI Product Version:iOS 17.2 RC Seed (21C62)
Classification:Incorrect/Unexpected Behavior Reproducible:Always
 
I have attached a sample project demonstrating the use of the `sheet(isPresented:content:)` modifier. The content closure reads an `@State` variable. However, the `@State` variable’s value is stale when the content closure is executed.

This bug is likely related to the fact that the content closure is executed lazily, so a dependency on the `@State` variable is not set up. One workaround is for developers to use a closure capture list to capture the variable by value so that the `@State` variable is accessed during closure creation, correctly setting up the dependency. However, it is quite unfortunate for developers to have to know this. There must be a way that SwiftUI itself can fix this bug.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!