Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

kotlin

I wrote a condition as below

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

As my subscriber could be null. The above title error displayed. So I cast it as below

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

It looks not as nice. Is there a better way of doing this?

like image 635
Elye Avatar asked Apr 07 '16 02:04

Elye


People also ask

How do you use Boolean in Kotlin?

Kotlin Boolean Expression A Boolean expression returns either true or false value and majorly used in checking the condition with if...else expressions. A boolean expression makes use of relational operators, for example >, <, >= etc.

How do I check my Boolean Kotlin?

An idiomatic way to do it in Kotlin would be using the when expression: var b: Boolean? = null when (b) { true -> ... false -> ... else -> ... }

How do I return a Boolean in Kotlin?

If the key is present, then the […] operator will return the corresponding Boolean value; but if the key is not present (which could happen if the map doesn't contain a "default" key), it returns null. However, the variable you're trying to assign it to is of type Boolean , which doesn't allow nulls.

Does null equal false Kotlin?

TL;DR Kotlin has the === operator for referential integrity and none of true , false or null references the same thing. Don't forget that, when comparing nullable booleans.

How do you declare a Boolean type in Kotlin?

A boolean type can be declared with the Boolean keyword and can only take the values true or false: Just like you have learned with other data types in the previous chapters, the example above can also be written without specifying the type, as Kotlin is smart enough to understand that the variables are Booleans:

What are primitive data types in Kotlin?

In Kotlin, there are no primitive data types such as int or boolean in Java. Instead, we have “boxed” primitive types like Int and Boolean. Kotlin’s nullable Boolean type Boolean? is pretty similar to Java’s Boolean type.

What is Kotlin programming language?

Basically, kotlin is strong, and also it is the statically typed programming language for each and every variable that deals with the expression and it’s the type that can be used for compiled programming language.

How do I check if a Boolean expression is true?

A Boolean expression returns a Boolean value: true or false. You can use a comparison operator, such as the greater than ( >) operator to find out if an expression (or a variable) is true:


2 Answers

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.

like image 145
AndroidEx Avatar answered Sep 19 '22 12:09

AndroidEx


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

like image 20
Jakub S. Avatar answered Sep 19 '22 12:09

Jakub S.