I have the following that returns a Flow<List<Events>>. But I want return the List<Events>> from the flow instead of Flow<List>
val events = eventDao.getEventsFromTimeStamp(startTimeStamp, endTimeStamp)
.map { listOfEventEntity ->
listOfEventEntity.map { eventEntity ->
eventEntity.toEvent()
}
}
I have tried using the following operators
val listOfEvents = events.single()
val listOfEvent = events.toList().flatten()
This is my eventDao that return a flow
fun getEventsFromTimeStamp(startTimeStamp: Long, endTimeStamp: Long): Flow<List<EventEntity>>
If both
single()andtoList().flatten()aren't working for you then theFlowis not emitting any items.single()should results in:FlowNoSuchElementExceptionfor empty flowIllegalStateExceptionfor flow that contains more than one element.This last case is possible if for example you're streaming a query from Android Room, SqlDelight or Kafka but the stream remains empty forever since the table remains empty or no events are published to Kafka.
toList().flatten()should result in:Listof allListvalues in theFlow.To make an infinite stream finite you can use the
takeoperator.Let's take an example:
In case that the stream is infinite but never emits a value, this will hang. Similar to for
single. This could be fixed using a timeout to prevent it hanging forever. It's not an ideal situation, since it will always take the timeout duration to get a result, but at least it will terminate.