-1

hello) . Need some help.I know how to transfer data from A class to B class, but i dont know how to transfer data from subclass A.A1 - which included A class, to subclass B.B1 which included B class.

My A class:

data class SpaceXProperty(
   val  flight_number: Int,
   val launch_year: Int,
   val links: Links
){

   data class Links(val mission_patch: String?)
}

My B class:

data class Models(val flightNumber: Int,
                  val mlinks: Mlinks
)
{
    data class Mlinks(val mission_patch: String?)
}

I created next function

fun List<SpaceXProperty>.asDomainModel(): List<Models>{
   return map {
      Models(
         flightNumber = it.flight_number,
         llinks = it.links //here i got error type mismatch "required: Models.Mlinks found: SpaceXProperty.Links"

      )
   }
}

So thats to avoid this - i change B class to next

data class Models(val flightNumber: Int,
                  val llinks: SpaceXProperty.Links
                  
)

I new at kotlin, so dont know how correctly mapping data from one subclass to another. Help please.

i trying to put next function at fun List.asDomainModel(), but its dont work.

fun List<SpaceXProperty.Links>.asDDomainmodel(): List<Models.Llinks>{
   return map {
     Models.Llinks(
         mission_patch = it.mission_patch) }
}

Totally i getting data from "https://api.spacexdata.com/v3/launches" and want paste this data to another data class like "repository". Here Moshi cannot parse nullable i get help that i need to use subclass, but at next step i got problems.

bboy
  • 55
  • 5

1 Answers1

1

Couple of minor errors:

  • In the first asDomainModel function you used llinks property instead of mlinks
  • Trying to assign object of Links to Mlinks

Following code resolves the issue

fun List<SpaceXProperty>.asDomainModel() = map {
    Models(
        flightNumber = it.flight_number,
        mlinks = Models.Mlinks(it.links.mission_patch)
    )
}

The original classes stays as is

data class SpaceXProperty(
    val flight_number: Int,
    val launch_year: Int,
    val links: Links) {
    data class Links(val mission_patch: String?)
}

data class Models(val flightNumber: Int, val mlinks: Mlinks) {
    data class Mlinks(val mission_patch: String?)
}
sidgate
  • 14,650
  • 11
  • 68
  • 119