I have a transaction repository from which I can get a Flow<List<Transaction>>.
Based on this data, I need to create a Flow<List<Source?>>.
The transaction has sourceId which can be passed to sourceRepository.getSource() which returns a source if it exists or else it returns null.
ViewModel code
var transactions: Flow<List<Transaction>> = transactionRepository.transactions
var sourceList: Flow<List<Source?>> = flow {
transactions.map {
it.map { transaction ->
if (transaction.sourceId != null) {
sourceRepository.getSource(transaction.sourceId)
} else {
null
}
}
}.collect {
emit(it)
}
}
Is there any way to change the map of map and make this code less?