LazyVStack Error using ForEach

My app crashes on iPhone/iPad Simulator when I'm using 2 ForEach in a LazyVStack. In the Canvas is works perfectly fine.

Error message is: 'Fatal error: each layout item may only occur once'

Minimum example:
Code Block swift
struct ContentView: View {
    var body: some View {
        ScrollView {
            LazyVStack{
                ForEach(0...2, id: \.self) { _ in
                    Section {
                        ForEach(0..<2, id: \.self) { idx in
                            Text("Section Cell \(idx)")
                        }
                    }
                }
            }
        }
    }
}

If you change the LazyVStack with Vstack it work and if you delete one of the ForEach it work also on the simulator.
Answered by mtsrodrigues in 615116022
The problem is the fact that in your code you end up with repeated identifiers and I believe that LazyVStack requires the ids to be unique. I managed to make it work using a UUID as an identifier:

Code Block swift
struct ContentView: View {
    var body: some View {
        ScrollView {
            LazyVStack{
                ForEach(0...2, id: \.self) { section in
                    Section {
                        ForEach(0..<2) { item in
                            Text("Section: \(section), Item: \(item)")
                                .id(UUID())
                        }
                    }
                }
            }
        }
    }
}


Accepted Answer
The problem is the fact that in your code you end up with repeated identifiers and I believe that LazyVStack requires the ids to be unique. I managed to make it work using a UUID as an identifier:

Code Block swift
struct ContentView: View {
    var body: some View {
        ScrollView {
            LazyVStack{
                ForEach(0...2, id: \.self) { section in
                    Section {
                        ForEach(0..<2) { item in
                            Text("Section: \(section), Item: \(item)")
                                .id(UUID())
                        }
                    }
                }
            }
        }
    }
}


LazyVStack Error using ForEach
 
 
Q