I have 2 lists on different screens. It sorts the other list according to the data in the list I printed first. For example, if "name" is written in the list, it pulls data from firebase and lists it according to the "name" data in the other list. But the first time I click on the list, the empty list comes up and the next time I click, the data comes up according to the first click.
@ObservedObject var findStadium=FindStadium()
@State var selectedTown=""
@State var stadiumNameArray=[String]()
var body: some View {
NavigationView {
List(findStadium.townArray){ towns in
NavigationLink(destination: StadiumNameView().onAppear{
selectedTown=towns.town
let firestoreDatabase=Firestore.firestore()
firestoreDatabase.collection("Stadiums").order(by: "Name",descending: false).addSnapshotListener { (snapshot, error) in
if error != nil {
print(error?.localizedDescription ?? "Error")
} else {
if snapshot?.isEmpty != true && snapshot != nil {
stadiumnameeeee.removeAll(keepingCapacity: false)
print(stadiumnameeeee)
for document in snapshot!.documents {
if let Name = document.get("Town") as? String {
if Name==self.selectedTown {
let stadiumName=document.get("Name") as! String
self.stadiumNameArray.append(stadiumName)
stadiumnameeeee.append(stadiumName)
print(stadiumName)
print(stadiumnameeeee)
}
}
}
}
}
}
}) {
Text(towns.town)
}
}
.navigationBarTitle("İstanbul",displayMode:.large) //başka şehirler eklenirse düzenle
}
}}
and other view
var body: some View {
VStack{
List(stadiumnameeeee,id:\.self) { i in
NavigationLink(destination: SelectedStadiumView()){
Text(i)
}
}
}
}}
where am i doing wrong? please help!