safe call question on nullable variable: Let’s sa...
# announcements
w
safe call question on nullable variable: Let’s say I have a variable of type
List<String>?
and I want to check that it contains at least one element. Is it possible to use just one condition check? I’ve tried:
Copy code
val t: List<String>? = null
println(t?.size != 0)
and this prints
true
, which is not what I want. I tried:
!t?.size?.equals(0)
but I can’t use this in an
if
because it’s
Boolean?
.
a
t?.isEmpty() ?: true
w
👍
a
or in your case
t?.isNotEmpty() ?: false
returns true if the list exists and has at least one element