I'm trying to map a network object to a domain object with a .map in the response, but compiler says me that .map is an unresolved reference. What is wrong?
suspend fun getAllPlayersFromNetwork(): PlayersDomainModel {
val response: PlayersNetworkModel = api.getAllPlayers()
**return response.**map** { it.toDomain() }**
}
This is the data class of the network part
data class PlayersNetworkModel(
@SerializedName("data") var data: MutableList<DataNetworkModel> = mutableListOf(),
@SerializedName("meta") var meta: MetaNetworkModel? = MetaNetworkModel()
){
constructor() : this(mutableListOf(), MetaNetworkModel())
}
This is the object of the domain
data class PlayersDomainModel(
var data: MutableList<DataDomainModel> = mutableListOf(),
var meta: MetaDomainModel? = MetaDomainModel()
)
and this is the transform method to map
fun PlayersNetworkModel.toDomain(): PlayersDomainModel {
val mapMeta : MetaDomainModel
meta.let {
mapMeta = MetaDomainModel(meta?.totalPages, meta?.currentPage, meta?.nextPage, meta?.perPage, meta?.totalCount)
}
val mapData : MutableList<DataDomainModel> = mutableListOf()
data.let {
for(item in data){
val mapTeam : TeamDomainModel = TeamDomainModel(item.team?.id, item.team?.abbreviation, item.team?.city, item.team?.conference, item.team?.division, item.team?.fullName, item.team?.name)
mapData.add(DataDomainModel(item.id, item.firstName, item.heightFeet, item.heightInches, item.lastName, item.position, mapTeam, item.weightPounds))
}
}
return PlayersDomainModel(mapData, mapMeta)
}
CodePudding user response:
I'm not sure why simple data class suppose to have a map method.
Instead of mapping:
return response.map { it.toDomain() }
just use toDomain()
on the response:
return response.toDomain()