How do I run a block of code if a nullable type is null?

You can use the elvis operator and evaluate another block of code with run { ... }:

data?.let {
    // execute this block if not null
} ?: run {
    // execute this block if null
}

But this seems not to be quite as readable as a simple ifelse statement.

Also, you might find this Q&A useful:

  • In Kotlin, what is the idiomatic way to deal with nullable values, referencing or converting them

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)