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

I wrote a condition below

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

How can my subscriber be empty. The above title error is displayed. So I threw it below

    if (subsriber?.isUnsubscribed as Boolean && isDataEmpty()) {
        loadData()
    }

It doesn’t look so good. Is there a better way to do this?

+4
source share
2 answers

I usually resolve this situation with the operator ?::

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

Thus, if subscriberthe same null, subsriber?.isUnsubscribedalso null, and subsriber?.isUnsubscribed ?: falseis estimated as falsethat, we hope, will be expected results, otherwise switch on ?: true.

, as Boolean , null.

+11

: kotlin.Boolean. : kotlin.Boolean? :

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

, ,

+2

All Articles