I have a dynamic SwiftUI List that populates utilizing an enum for States. Then it cycles through my dictionary of data looking for a state match to display data. All of this works perfectly. What I am looking to do but struggling with is to hide those states that are then empty.
The section of code in question is:
@State private var bucketList: [BucketListItems] = []
...
List {
ForEach(USStates.allCases) { states in
Section {
ForEach (bucketList) { item in
if (item.state == states.abrev) {
NavigationLink(destination: ItemDetails(blItem: item.recordID)) {
Label(item.name, systemImage: item.monogram)
.frame(height: 50)
.foregroundColor(item.completedate=="0000-00-00" ? .white : .red)
}
}
}
} header: {
Text(states.rawValue)
}
}
}
When a state doesn't have a match, then I want that entire section to be hidden.
Some additional relevant code:
struct BucketListItems: Encodable, Decodable, Identifiable, Hashable {
let id = UUID()
var recordID: Int
var url: String
var user: Int
var name: String
var category: String
var state: String
var latitude: String
var longitude: String
var description: String
var completedate: String
}
if
clause aroundSection
that checks to see if there's contentView
's initif (bucketList.contains(where: {$0.state == states})) { Section { ...
Adjust the equality as needed.