https://kotlinlang.org logo
#getting-started
Title
# getting-started
z

Zun

06/06/2021, 1:41 PM
Not new to Kotlin but new to Flow. Is there a way to rewrite the following code to make it shorter, or do I need to resort to writing my own extension? I need to emit null when the list is empty
onEmpty{emit(null)}
doesn't work
k

knthmn

06/06/2021, 2:01 PM
Flow.onEmpty()
runs the lambda when the flow finishes without emitting any elements, it has nothing to do with the list being empty
also you are trying to transform
showDao.loadHomeScreen()
, you can just use
Flow.map()
z

Zun

06/06/2021, 2:02 PM
I need to emit a list only when it contains entries. If it is empty, I need to emit null
k

knthmn

06/06/2021, 2:02 PM
i would write the whole thing as
Copy code
showDao.loadHomeScreen().map { it.takeIf { it.isNotEmpty() } }
✔️ 2
👍 1
3 Views