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)
                    }
                }
            }
        }
    }
}

Strange... In my project, NavigationLink is not working too. It says extra argument in call. Here is my code:

NavigationLink { Text("Sign up") } label: { HStack { Text("Don't have an account?") Text("Sign Up") .fontWeight (.semibold) } .font(.footnote) } .padding(.vertical, 16)

I have the same issue. I am new to iOS development and haven't got any solution to this problem yet.

But I have an understanding that the problem is not with NavigationLink. It loads the new view but somehow it was not able to disapper the current view and it stuck there. You can check this out by using onAppear and onDisapper on the current view and view you want to load.

There is something to do with NavigationStack and @Environment(.dismiss). As NavigationLink will work if you remove NavigationStack or replace it with NavigationView.

If someone got the solution with NavigationStack please let me know

I am running into the same issue with iOS 17 and switch from NavigationView to NavigationStack. We are not using NavigationLink but navigationDestination(isPresented:) , but the issue is the same.

You need to place your

@Environment (\.dismiss) var dismiss

directly into your .sheet

SwiftUI NavigationLink not working when I add @Environment(\.dismiss) to my view
 
 
Q