Learn Courses My Dashboard

Correct way to handle a list, when no items are available?

I’m currently working on a to-do list app. When there are no to-dos, the used list-element shows a grey rectangle. Because that’s not very cute and expresses nothing, I have tried to figure out a workaround. Here’s the solution I have found:

// ...
if todos.count == 0 {
    Text("Currently exist no to-dos")
    Spacer()
} else {
    List(todos) { todo in
        NavigationLink {
            Details(todo: todo)
        } label: {
            Text(todo.text ?? "")
       }
   }
}
// ...

Is there are a better way to deal with a list, when no list-items exist?

I think that is a perfectly reasonable solution and is pretty much what I would have done.

1 Like

One thing is instead of todos.count you can do todos.isEmpty

But otherwise this works :+1:

1 Like