App crashes when List has fewer items than before

Hi all, I'm fairly new to Swift and SwiftUI and am currently trying to build a trello app for macOS. things have been mostly fine so far, but now I am trying to understand why my app crashes when you switch between boards with an Index out of range error:

Swift/ContiguousArrayBuffer.swift:575: Fatal error: Index out of range
2022-10-18 13:16:30.178158+0200 trello[21927:17216201] Swift/ContiguousArrayBuffer.swift:575: Fatal error: Index out of range

It happens when any list in the new board has fewer cards (items) than in the old board The app always crashes in an onReceive that monitors an update to the card (so that I can do things like change a cards background color when a new label is applied or other things) I've tried using both the swiftui List directly or a ForEach, i'm just not sure what I am doing wrong

I have created a branch with the minimal code that seems to be causing issues on my repo:

how can I resolve this problem?

If your first board has, say, 10 cards, and you switch to a new board with only 8 cards, you're saying it crashes here because there are fewer cards than before? This sounds like you should be removing the board and creating a new board with the new list, not re-using the old board. You need to update the number of cards when you switch so that the List draws the right number of cards. Are you sure you're passing the current number of cards into your TrelloListView()?

It seems like I entered the first link wrong: the crash location is here: https://github.com/Rukenshia/trello/blob/troubleshooting-board-switch-crash/trello/views/CardView.swift#L74

App crashes when List has fewer items than before
 
 
Q