theapache64
03/01/2022, 3:07 PMB
null?.let {
println("A")
} ?: println("B")
but the below code doesn’t render B
null?.let {
Text(text = "A")
} ?: Text(text = "B")
Why? 🤔theapache64
03/01/2022, 3:07 PMFilip Wiesner
03/01/2022, 3:11 PMAlexander Maryanovsky
03/01/2022, 4:01 PMAlexander Maryanovsky
03/01/2022, 4:02 PMtheapache64
03/01/2022, 4:45 PMtheapache64
03/01/2022, 4:46 PMPaul Woitaschek
03/01/2022, 6:18 PMmattinger
03/01/2022, 6:22 PMAlexander Maryanovsky
03/01/2022, 6:49 PMlet
happens to be null
, you will have the part after the ?:
evaluated as well.theapache64
03/01/2022, 6:49 PMclass User(val job: Job?)
class Job(val title: String?)
val user: User? = User(job = null)
// Option #1
user?.job?.title?.let { Text("Job title is $it") } ?: Text("No job title found")
// Option #2
val jobTitle = user?.job?.title
if (jobTitle == null) {
Text("Job title is $jobTitle")
} else {
Text("No job title found")
}
Here, I agree #2 gives a little more readability, but #2 helps us to avoid declare a local variable. What problem do you see with #1 ? 🤔Alexander Maryanovsky
03/01/2022, 6:49 PMtheapache64
03/01/2022, 6:50 PMAlexander Maryanovsky
03/01/2022, 6:53 PMtheapache64
03/01/2022, 6:56 PM