Home > Software engineering >  Issue using a ListView inside a FormView
Issue using a ListView inside a FormView

Time:10-23

Essentially I would like to add a section to my Form that is a dynamic list, why doesn't this work inside a Form, any workaround?

        Form {
            Section("My List") {
                ForEach(list, id: \.self) { listItem in
                    Text(listItem.name)
                }
                .onAppear {
                    APICalls().getData(url: "https://test.com") { (info: [Couriers], _) in
                        self.list = info
                        if list == [] {
                            print("No Data")
                        } else {
                            
                        }
                    }
                }
                
            }

CodePudding user response:

move the .onAppear(...) outside the Form, like this (works for me)

var body: some View {
    Form {
        Section("My List") {
            ForEach(list, id: \.self) { listItem in
                Text(listItem.name)
            }
        }
    }
    .onAppear {
        APICalls().getData(url: "https://test.com") { (info: [Couriers], _) in
            self.list = info
            if list == [] {
                print("No Data")
            } else {
                
            }
        }
    }
}
  • Related