NavigationLinks to "Details" page do not work after data changes

I'm running into this error, when the underlying SwiftData object changes (gets an object added to it):

*" A NavigationLink is presenting a value of type “Project” but there is no matching navigationDestination declaration visible from the location of the link. The link cannot be activated.

Note: Links search for destinations in any surrounding NavigationStack, then within the same column of a NavigationSplitView. "*

For instance, when I first fire up the simulator, clicking on any existing project correctly takes me to its details page. However, as soon as I add a new project or delete a project, none of the navigation links work any more. If I reload the simulator, they all work again! Makes me suspect it's an error with not recomputing the links when the underlying data change, but I can't figure out what I need to change, as everything looks correct. Any help would be appreciated!

ContentView:

struct ContentView: View {
    @Environment(\.modelContext) var modelContext
    @Query(sort: \Project.endDate) var projects: [Project]

    var body: some View {
        NavigationStack {
            Group {
                if projects.isEmpty {
                    ContentUnavailableView("Enter your first project.", systemImage: "clipboard")
                } else {
                    List {
                        ForEach(projects, id: \.id) { project in
                            NavigationLink(value: project) {
                                HStack {
                                    DaysTile(
                                        days: project.numWorkDaysNowUntilEnd,
                                        description: "Work Days Remaining"
                                    )
                                    .padding(.trailing, 5)

                                    VStack(alignment: .leading) {
                                        Text(project.name)
                                            .font(.title)

                                        Text("Start: \(project.startDate.formatted(date: .abbreviated, time: .omitted))")
                                            .foregroundStyle(.secondary)

                                        Text("End: \(project.endDate.formatted(date: .abbreviated, time: .omitted))")
                                            .foregroundStyle(.secondary)
                                    }
                                }
                            }
                        }
                        .onDelete(perform: deleteProjects)
                    }
                    .navigationDestination(for: Project.self) { project in
                        ProjectDetailView(project: project)
                    }
                }
            }
            .navigationTitle("Project Tracker")
            .toolbar {
                NavigationLink {
                    AddProjectView()
                } label: {
                    Label("Add New Project", systemImage: "plus")
                }
            }
        }
    }

    func deleteProjects(at offsets: IndexSet) {
        for offset in offsets {
            let project = projects[offset]
            modelContext.delete(project)
        }
    }
}

#Preview {
    ContentView()
        .modelContainer(for: Project.self, inMemory: true)
}

Accepted Reply

Fixed!

I found that I had a second NavigationStack on my DetailView. Removing it, so there was only one in ContentView, fixed the problem!

Replies

Fixed!

I found that I had a second NavigationStack on my DetailView. Removing it, so there was only one in ContentView, fixed the problem!