A navigationDestination for “app.Type" was declared earlier on the stack

37 views Asked by At

I understand what this means conceptually, but I don't understand why I'm not navigating to my ThirdView in response to it.

I'm trying to navigate two levels into the NavigationStack. But I keep pushing the second view on top of itself.

Here's my code:

Model

struct Person: Identifiable, Hashable {
    var id = UUID()
    var firstName: String
    var lastName: String
}

View

struct Person: View {    
    @EnvironmentObject var appState: AppState
    
    var body: some View {
        NavigationStack(path: $appState.path) {
            ZStack {
                Color.theme.color
                    .ignoresSafeArea()
                
                ScrollView(showsIndicators: false) {
                    PersonListView()
                }
            }
            .navigationTitle("People")
        }
    }
}

Person List View


struct PersonListView: View {
    
    let people = Person.persons.sorted(by: { $0.firstName < $1.firstName })
        
    var body: some View {
        VStack {
            VStack(spacing: 25.0) {
                ForEach(people) { person in
                    NavigationLink(value: person) {
                        VStack(spacing: 10.0) {
                            HStack {
                                Text(person.firstName)
                                    .foregroundColor(.black)
                                    .font(.system(size: 20.0, weight: .semibold))
                                    .padding(.top)
                                Spacer()
                            }
                            HStack {
                                Text(person.lastName)
                                    .foregroundColor(.gray)
                                    .font(.subheadline)
                                Spacer()
                            }
                        }
                        .frame(width: 350.0)
                        .cornerRadius(10.0)
                        .shadow(radius: 1.0)
                        .padding()
                        .background(.white)
                    }
                }
                .navigationDestination(for: Person.self) { person in
                    PersonDetailView(person: person)
                }
            }
        }
    }
}

I have a var person: Person on my DetailView and an environment variable of path on my third view. PersonView is embedded in a custom TabView, if that's helpful.

0

There are 0 answers