如何在<Unit> kotlin 中将列表类型更改为正确的列表类型



我有一个问题,只获得信息与传递的coinList值。首先,我想让coinList变成List<Coin>而不是List<Unit>,我不知道如何改变它。我想比较it.idcoinList,如果它们有相同的值,然后把它传递给函数硬币。提前感谢!

class GetCoinsUseCase @Inject constructor(
private val repository: CoinRepository
){
private val coinList = listOf("btc-bitcoin", "usdt-tether", "eth-ethereum")
operator fun invoke(): Flow<Resource<List<Coin>>> = flow{
try{
emit(Resource.Loading<List<Coin>>())
val coins = repository.getCoins().map { if (it.id.equals(coinList)){ it.toCoin() }}
emit(Resource.Success<List<Coin>>(coins))
}catch (e: HttpException){
emit(Resource.Error<List<Coin>>(e.localizedMessage ?: "Error occurred"))
}
}
}

也许这样做会起作用,首先做一个只检索相关项的过滤器,然后映射

val coinsList = arrayListOf<String>("foo", "bar")
val coins = repository.getCoins().filter { it.id in coinsList }.map {
it.toCoin()
}

最新更新