Not new to Kotlin but new to Flow. Is there a way ...
# getting-started
z
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
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
I need to emit a list only when it contains entries. If it is empty, I need to emit null
k
i would write the whole thing as
Copy code
showDao.loadHomeScreen().map { it.takeIf { it.isNotEmpty() } }
✔️ 2
👍 1