Have a look at the code below which is based on yours.
struct ContentView: View {
var stringArray: [String] = ["Gluon", "Boson", "Muon", "Positron", "Neutrino"]
@State var arrayToBeFilled: [String] = []
var body: some View {
VStack {
Button("ADD"){
let random = Int.random(in: 0...4) // Does not need to be a var since it is not being changed
arrayToBeFilled.append(stringArray[random])
}
List(arrayToBeFilled, id: \.self){ item in
Text(item)
}
}
.padding()
}
}
#Preview {
ContentView()
}
Some key points to note.
The body property of a view should only have code in it that results in a View. Declarations of variables (properties) should be inside the struct and are typically before the body property.
The array you have defined as arrayToBeFilled needs to be defined as a @State property. @State is a special SwiftUI property wrapper that enables a property of a struct to be mutable (able to be changed) whereas under normal circumstances the property of a struct cannot be changed.
If there is anything you don’t understand then by all means ask further questions.