I am working on my first app to display a list of meals and their details. I have simplified the code for my question here but essentially a list of meals is presented and I should be able to click on one to go to a child view. I am experiencing a weird behavior where the first time I click on a meal the child comes up blank. If I then click on another meal and click back. the child view displays fine. any help you can give me would be greatly appreciated.
import SwiftUI
struct ContentView: View {
@State var meals: [Meal] = [
Meal(name: "filet steak"),
Meal(name: "pepperoni pizza"),
Meal(name: "pancakes"),
Meal(name: "full breakfast")
]
@State private var selectedMeal: Meal?
@State private var isShowingMealForm = false
var body: some View {
NavigationStack {
List {
ForEach(meals) { meal in
HStack {
Text(meal.name)
.font(.headline)
}
.onTapGesture {
self.selectedMeal = meal
isShowingMealForm = true
}
}
}
}
.sheet(isPresented: $isShowingMealForm) {
if let m = selectedMeal {
Text(m.name)
}
}
}
}
class Meal: Identifiable {
var id: UUID = UUID()
var name: String = ""
init() {}
init(id: UUID = UUID(), name: String)
{
self.id = id
self.name = name
}
}