Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add two conditions using let in Android Kotlin

Tags:

android

kotlin

I want to use let to check two conditions Lets say if I had to use if then this is the condition

if (it.data != null && !it.data!!.name.isEmpty()) {}

How can I convert it to use let I know that to check null this is what we do

it.data?.let {}

but I don't know how to check the second part which is if a string is empty or not. Please keep in mind that I have to check that data is not null AND data.name is not empty

Any help will be highly appreciated

like image 779
Zohab Ali Avatar asked Dec 18 '22 11:12

Zohab Ali


1 Answers

For your specific case you could use takeIf

it.data
   ?.takeIf { data ->  data.name.isNotEmpty() }
   ?.let { data -> ...}
like image 171
Feedbacker Avatar answered Dec 21 '22 09:12

Feedbacker