'One working ScrollView example and one refusing example

This is a working example of a ScrollView:

    var body: some View {
    
    ScrollViewReader { proxy in
        VStack {
            Button("Warp to line 10 on top") {
                proxy.scrollTo(10, anchor: .top)
            }
            List(0..<100, id: \.self) { i in
                HStack {
                    Text("Example \(i)")
                        .id(i)
                    Spacer()
                    Text("Example \(i)")
                        .id(i)
                }
            }

I tried another example that I want to incorporate in my app, but that example doesn't work:

    var body: some View {
    
    ScrollViewReader { proxy in
        VStack {
            Button("Warp to line 10 on top") {
                proxy.scrollTo(10, anchor: .top)
            }
            List {
                ForEach(totalWeights, id: \.self) { totalWeight in
                    NavigationLink(destination: WeightDetailView()) {
                        HStack {
                            Text("Day: \(totalWeight.day)")
                                .foregroundColor(totalWeight.measured ? .green : .red)
                            Spacer()
                            Text("17 Apr")
                            Spacer()
                            Text("\(formatVar2(getal: totalWeight.weight)) gr")
                            Spacer()
                            Text("\(formatVar1(getal: totalWeight.prediction)) %")
                        }
                    }
                }
            }

What am I not seeing?

Based on the remark of xTwisteDx I made another version of the code. But still the same problem...

                    ScrollViewReader { proxy in
                VStack {
                    Button("Jump to #10") {
                        proxy.scrollTo(10, anchor: .top)
                    }
                    List {
                            ForEach(totalWeights, id: \.self) { totalWeight in
                                NavigationLink(destination: WeightDetailView()) {
                                    HStack {
                                    Text("Day: \(totalWeight.day)")
                                        .foregroundColor(totalWeight.measured ? .green : .red)
                                        .id(totalWeight.day)
                                    Spacer()
                                    Text("17 Apr")
                                            .id(totalWeight.day)
                                    Spacer()
                                    Text("\(formatVar2(getal: totalWeight.weight)) gr")
                                            .id(totalWeight.day)
                                    Spacer()
                                    Text("\(formatVar1(getal: totalWeight.prediction)) %")
                                            .id(totalWeight.day)
                                            .background(.thinMaterial, in: RoundedRectangle(cornerRadius: 10))
                                        .padding(EdgeInsets(top: -20, leading: 0, bottom: 0, trailing: 0))
                                }
                            }
                        }
                    }
                }
            }

totalWeight is of type ActualWeight, and I made it hashable. Also day: Int, so it should match 10 in the code. If I click on the button I want to scroll the list to line 10.

struct ActualWeight: Hashable, Codable {

        var day: Int
        var weight: Double
        var measured: Bool
        var dailyLoss: Double
        var weightLoss: Double
        var prediction: Double
    }


Solution 1:[1]

Try following

var body: some View {
    ScrollViewReader { proxy in
        VStack {
            Button("Warp to line 10 on top") {
                proxy.scrollTo(10, anchor: .top)
            }
            List {
                ForEach(0..<totalWeights.count, id: \.self) { i in
                    NavigationLink(destination: WeightDetailView()) {
                        HStack {
                            Text("Day: \(totalWeights[i].day)")
                                .foregroundColor(totalWeights[i].measured ? .green : .red)
                            Spacer()
                            Text("17 Apr")
                            Spacer()
                            Text("\(formatVar2(getal: totalWeights[i].weight)) gr")
                            Spacer()
                            Text("\(formatVar1(getal: totalWeights[i].prediction)) %")
                        }
                    }
                }
            }
        }
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Suyash Medhavi