I want to negate the following expression:
return SpUtils.loadEMail()?.isEmpty() ?: false
If i add a ! before the expression, like
return !SpUtils.loadEMail()?.isEmpty() ?: false
The IDE(Android Studio) tells me
Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type kotlin.Boolean?
How do I negate this kinds of expressions?
Boolean to String You can use toString() function to convert a Boolean object into its equivalent string representation. You will need this conversion when assigning a true or false value in a String variable.
Kotlin's nullable Boolean type Boolean? is pretty similar to Java's Boolean type. Both may have a true, false, or null value.
The NOT Operator: ! The logical NOT operator ( ! ) evaluates the value of a Boolean expression and then returns its negated value.
You have problem with nullable reference.
SpUtils.loadEMail()?.isEmpty()
This code produces value of type Boolean? that's mean expression can return an instance of Boolean or null.
I suggest following code to solve your problem:
return !(SpUtils().loadEMail()?.isEmpty() ?: false);
You trying negate Boolean? instead of Boolean, that elvis operator returns!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With