I'm using a ForEach to loop through an array of saved user input and display those, which displays everything correctly, unless I add a new input to this array, then the new item isn't displayed unless I navigate to another View then come back.
I've tried everything, from changing .id with .self, but nothing works.
Any idea guys? Thank you!
EDIT: It looks like it's my sheet that prevents my view from updating, I've tried simply adding the sheet code in my view and now the ForEach updates in real time, is there something I'm missing regarding how sheets could get in the way?
struct AnswersView: View {
@EnvironmentObject var vm: BrainstormViewModel
@Environment(\.presentationMode) var presentationMode
@State var mBIndex: Int
@State var substitute: [String] = []
@State var showSheet: Bool = false
@State var updater: Bool = false
var body: some View {
ScrollView {
VStack(alignment: .leading) {
Text("How could I...")
Text(vm.brainstormArray[mBIndex].title)
.font(.system(size: 32, weight: .bold, design: .rounded))
.multilineTextAlignment(.leading)
Button {
showSheet.toggle()
} label: {
ButtonSubview(icon: "play.fill", text: "Continue brainstorm")
.environmentObject(BrainstormViewModel())
}
ForEach(vm.brainstormArray[mBIndex].answersArray, id: \.self) { index in
Text(index.name)
.font(.system(size: 20, weight: .bold, design: .rounded))
.padding(.top, 30)
HStack {
Text(index.answer[0])
.padding()
Spacer()
}
.background(Color.MyTheme.orangeLight)
.cornerRadius(16)
}
}
.fullScreenCover(isPresented: $showSheet) {
BrainstormView(mBIndex: mBIndex)
}
.navigationBarTitleDisplayMode(.inline)
}
.frame(maxWidth: .infinity)
.padding()
.background(Color.white)
}
}
id: \.selfis a mistake, ForEach works with Identifiable data you need to edit your model data struct to conform to that. Also we don't use view model objects in SwiftUI but yours actually looks like the model which is fine.