Im trying to a new value to an array and have a list get updated based on the array update on a button press but nothing happens when I click the button

var body: some View {

    var stringArray:[StringType] = [StringType(stringName: "Gluon"),StringType(stringName: "Boson"),StringType(stringName: "Muon"),StringType(stringName: "Positron"),StringType(stringName: "Neutrino")
    ]
  
        var arrayToBeFilled:[StringType] = []
  
        
    VStack {
        Button("ADD"){
           
            var random = Int.random(in: 0...4)
            
            arrayToBeFilled.append(stringArray[random])
            
            
        }
        
        List(arrayToBeFilled){ item in
        
            
            Text(item.stringName)
        }
        
        
        
        
        
    }
    .padding()
}

}

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.

Thank you so much it worked.