Kotlin: Required: kotlin.Boolean. Found: kotlin.Boolean?

Also is you have just Required: kotlin.Boolean. Found: kotlin.Boolean? you can do this:

when(something?.isEmpty()) {
    true ->  {  }
    false -> {  }
    null ->  {  }
}

Also if you are interested only in one simple conditional statement

if(something?.isEmpty() == true){
  this will only worked if not null && empty
}

i know it's answered question but for future viewers can be helpful


I usually resolve this situation with the ?: operator:

if (subsriber?.isUnsubscribed ?: false && isDataEmpty()) {
    loadData()
}

This way, if subscriber is null, subsriber?.isUnsubscribed is also null and subsriber?.isUnsubscribed ?: false evaluates to false, which is hopefully the intended result, otherwise switch to ?: true.

Also casting a nullable type with as Boolean is unsafe and will throw an exception if null is encountered.


Another way to resolve this issue is to explicitly check if the expression is true:

if (subsriber?.isUnsubscribed == true && isDataEmpty()) {
    loadData()
}

Tags:

Kotlin