Ahmed
10/22/2024, 10:31 AMNavHost
that contains polymorphic members … more in 🧵Ahmed
10/22/2024, 10:34 AM@Serializable
data class Detail(val model: DetailModel, val someFlags …)
Destination is defined as:
fun NavController.navigateToDetail(model: DetailModel) {
navigate(
route = Detail(
model = model,
detailType = when (model) {
is DetailModel.Detail1 -> …
is DetailModel.Detail2 -> … // Make decision from combination of flag
},
),
) {
popUpTo(Home) { inclusive = false }
}
}
Ahmed
10/22/2024, 10:36 AMDetailModel
is a sealed class, I am getting the
Route ….Detail could not find any NavType for argument model of type ….DetailModel - typeMap received was {}
Ahmed
10/22/2024, 10:39 AMPolymorphism
, I used this guide to modify my DetailModel
Ahmed
10/22/2024, 10:40 AMAhmed
10/22/2024, 10:41 AMDetailModel
in question
@Immutable
@Serializable
sealed class DetailModel {
abstract val name: String
abstract val slug: String
@Serializable
@SerialName("detail1")
data class Detail1(
override val slug: String,
override val name: String,
val x …
) : DetailModel()
@Serializable
@SerialName("detail2")
data class Detail2(
override val name: String,
override val slug: String,
val y …
) : DetailModel()
}
Ahmed
10/22/2024, 11:05 AMNavType
. I am still unsure on how to modify it for a sealed hierarchy case.Ahmed
10/22/2024, 11:07 AMStylianos Gakis
10/22/2024, 11:57 AM@Serializable
along with all the instances https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/polymorphism.md#sealed-classesAhmed
10/22/2024, 1:44 PMtypeOf<FindMyAgentListingModel>() to object : NavType<FindMyAgentListingModel>
for typemap
and it works. Had to add @Parcelize
on the sealed class.
I may switch to the string
decode/encode solution you mentioned as I find it nifty and neat.Stylianos Gakis
10/22/2024, 2:26 PM