SwiftUI sheet doesn’t access the latest value of state variables on first appearance

It seems like the state variable are not properly updated when a sheet is displayed for the first time.

For instance with this code:

import SwiftUI

struct DemoView: View {
    @State var showDetails: Bool = false
    var body: some View {
        VStack {
            Button(action: {
              showDetails = true
            }) {
                Text("Show sheet")
            }
        }.sheet(isPresented: $showDetails){
            VStack {
                Text("showDetails: (showDetails ? "yes" : "no")")
            }
        }
    }
}

struct DemoView_Previews: PreviewProvider {
    static var previews: some View {
        DemoView()
    }
}

This will display “no” on first click, and “yes” on second, as showcased here:

Am I missing something? How can I make sure my state variables are properly read by the sheet view?

1

While there are answers here that show how to mitigate this issue, I feel none of them explains why the issue happens or why the fix works which is in my opinion more important then blindly using a fix.

Basically, it seems that SwiftUI will reevaluate the DemoView body if any @State property is accessed when it first evaluates it.
Since showDetails property is set only in the Button action callback and accessed in the sheet content view builder callback which are not executed in the body SwiftUI doesn’t appear to know that it needs to reevaluate the body when it’s updated on tap.

You can check that by adding a simple print in the body var like so:

struct DemoView: View {
    @State var showDetails: Bool = false
    var body: some View {
        print("Body evaluated")
        return VStack {
            Button(action: {
              showDetails = true
            }) {
                Text("Show sheet")
            }
        }.sheet(isPresented: $showDetails){
            VStack {
                Text("showDetails: (showDetails ? "yes" : "no")")
            }
        }
    }
}

After tapping the button there will be no body reevaluation. This is why showDetails value captured in the sheet is the initial false value.

If you were to use showDetails variable in the DemoView body, either displaying it, or just adding a simple print, you will see that the body is reevaluated and value you see in the sheet is up to date.

struct DemoView: View {
    @State var showDetails: Bool = false
    var body: some View {
        print("Body evaluated (showDetails)")
        return VStack {
            Button(action: {
              showDetails = true
            }) {
                Text("Show sheet")
            }
        }.sheet(isPresented: $showDetails){
            VStack {
                Text("showDetails: (showDetails ? "yes" : "no")")
            }
        }
    }
}

2

You see value on time of sheet creation. If you want to track parent view state create sheet subview with binding to that state, like below. Binding will update subview when subview will appear.

Tested with Xcode 12 / iOS 14
Re-tested with Xcode 13.3 / iOS 15.4

struct DemoView: View {
    @State var showDetails: Bool = false
    var body: some View {
        VStack {
            Button(action: {
              showDetails = true
            }) {
                Text("Show sheet")
            }
        }.sheet(isPresented: $showDetails){
            SheetDetailView(flag: $showDetails)
        }
    }
}

struct SheetDetailView: View {
    @Binding var flag: Bool
    var body: some View {
        VStack {
            Text("showDetails: (flag ? "yes" : "no")")
        }
    }
}

0

Sometimes it’s not possible to use @Binding. For example if we need to use a variable in the init block. My way:

struct DemoView: View {

    @State var showDetails = false
    @State var wtfGuys = false

    var body: some View {
        VStack {
            Button(action: {
                showDetails = true
            }) {
                Text("Show sheet")
            }
        }
                .sheet(isPresented: $showDetails) {
                    ZStack {
                        if wtfGuys {
                            VStack {
                                Text("showDetails: (showDetails ? "yes" : "no")")
                            }
                        }
                    }
                            .onAppear { wtfGuys = true }
                            .onDisappear { wtfGuys = false }
                }
    }
}

Example with init block:

struct DemoView: View {

    @State var showDetails = false
    @State var wtfGuys = false

    var body: some View {
        VStack {
            Button(action: {
                showDetails = true
            }) {
                Text("Show sheet")
            }
        }
                .sheet(isPresented: $showDetails) {
                    ZStack {
                        if wtfGuys {
                            DetailView(showDetails: showDetails)
                        }
                    }
                            .onAppear { wtfGuys = true }
                            .onDisappear { wtfGuys = false }
                }
    }
}

struct DetailView: View {

    let showDetails: Bool

    init(showDetails: Bool) {
        print(showDetails) // true
        self.showDetails = showDetails
    }

    var body: some View {
        Text("showDetails: (showDetails ? "yes" : "no")")
    }
}

One more case:

struct DemoView: View {

    @State var showDetails = false
    @State var detailsText = ""

    var body: some View {
        VStack {
            Button(action: {
                detailsText = "new text"
                showDetails = true
            }) {
                Text("Show sheet")
            }
        }
                // Here it is
                .onChange(of: showDetails) { _ in }
                .sheet(isPresented: $showDetails) {
                    Text("text: (detailsText)")
                }
    }
}

4

You do not have to use the state variable in the body, you can just hint to the sheet that it is dependent on the state variable like so:

import SwiftUI

struct DemoView: View {
    @State var showDetails: Bool = false
    var body: some View {
        VStack {
            Button(action: {
              showDetails = true
            }) {
                Text("Show sheet")
            }
        }.sheet(isPresented: $showDetails){ [showDetails] in <-- add this
            VStack {
                Text("showDetails: (showDetails ? "yes" : "no")")
            }
        }
    }
}

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật