2

I'm trying to show Texts from a ForEach loop after doing an if check. Even though I have VStack inside and outside the ForEach, the text objects appear to overlap. I'm exploring other options as filtering the array but don't understand how to apply that to my conditional. Please let me know if there's an easy way to either have the results inside a VStack or filter the data.

struct LikedView: View {
    @ObservedObject var users = getUserData()
    @ObservedObject var globalCount = GlobalCount()
    @ObservedObject var movies = getMoviesData()
    @State var likedArray = []
    @State var count = 0
    
    var body: some View {
        //Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
        ZStack{
            ForEach(self.users.datas) { alpha in
                ForEach(self.movies.datas) { movies in
                    VStack{
                        ForEach(alpha.loved, id: \.self) { item in
                            VStack{
                                if(movies.id == item){
                                    Text(item)
                                }
                            }
                        }
                    }
                }
            }
        }
        .onAppear{
            self.users.fetchData()
            self.movies.fetchData()
        }
        
    }
}

enter image description here

  • What are you trying to show here? – nicksarno Jan 04 '21 at 04:57
  • ForEach is not a operator it is view container. Next should be helpful https://stackoverflow.com/a/62103264/12299030. – Asperi Jan 04 '21 at 05:03
  • @nicksarno the strings are auto id's generated by firestore. I'm storing a list of liked movies which I'm hoping to show on a different view. This is that view and even though it is dynamically getting the liked movie id's, it's cramming them together. I'm just confused since it's my first time working in detail with ForEach and didn't expect this. – Abenezer Bayeh Jan 04 '21 at 06:09

1 Answers1

0

What is happening in your code is you are putting the forEach loop in a ZStack which will render the views on top of the previous one. Try to wrap the forEach loop within HStack or VStack then they will be properly positioned.