SwiftUI List not rendering

Viewed 28

I'm trying to display a simple list of text like so:

class ViewModel: ObservableObject {
    
    @Published var users = [UserModel]()
    ...
List(viewModel.users) { user in
    Text(user.name) //this line doesn't get executed
}
class UserModel: BaseMapperModel, Identifiable {
    var id: Int = -1
    var name: String = "Unknown"
}

When I break at the "List" line, I can see that viewModel.users has 2 objects:

▿ 2 elements
  ▿ 0 : <UserModel: 0x7f8e85ce8e10>
  ▿ 1 : <UserModel: 0x7f8e85ce9a10>

And when I po viewModel.users[0].nameI see a valid name. So why isn't the text getting displayed? Why does the List line get executed, but the Text line does not? The users have unique ids.

1 Answers

Not sure why List doesn't work, but this does:

ForEach(viewModel.users) { user in
    Text(user.name)
}
Related