suspend fun <T> Flow<T>.last(): T? {
var last: T? = null
collect {
last = it
}
return last
}
👍🏼 2
d
dave08
02/05/2020, 5:00 PM
Thanks!
j
jw
02/05/2020, 5:01 PM
you could also do
Copy code
suspend fun <T> Flow<T>.last(): T {
val marker = Any()
var last: Any? = marker
collect {
last = it
}
if (last === marker) throw NoSuchElementException()
return last as T
}
👍 4
jw
02/05/2020, 5:01 PM
depends on the behavior you want
d
dave08
02/05/2020, 5:03 PM
Nice 🙂! I also didn't know that
Any()
could be instantiated and used like that... also good to know!