Post

Replies

Boosts

Views

Activity

SwiftUI NavigationLink not working when I add @Environment(\.dismiss) to my view
In SwiftUI I have a view (CartView) that is being presented from a NavigationLink (this navigationLink is in a different view, HomeView). When I add the @Environment(.dismiss) to the current view (CartView). The NavigationLink with "Checkout" label does not work and the app just freezes when its tapped. If i remove the @Environment wrapper the issue goes away and everything works perfectly fine. Below I have added some of the code in my view. struct CartView: View { @Environment(\.dismiss) var dismiss @EnvironmentObject var viewModel: CartViewModel var body: some View { NavigationStack { ScrollView { if viewModel.items.isEmpty { Text(AppConfig.Cart.emptyCartMessage) .font(.largeTitle) } else { ForEach(viewModel.items, id: \.id) { item in ItemView(item: item) .environmentObject(viewModel) } NavigationLink { CheckoutView() .environmentObject(viewModel) } label: { Text("Checkout") .padding() .foregroundColor(.init(uiColor: .systemBackground)) .frame(maxWidth: .infinity) .frame(height: 45) .background( RoundedRectangle( cornerRadius: 10, style: .continuous ) .fill(Color(uiColor: .label)) ) .padding(.top) .padding(.horizontal) } } } } } }
4
2
2.6k
Jun ’23