Hi all - Working on a CMP app with type-safe nav, ...
# compose
d
Hi all - Working on a CMP app with type-safe nav, using 2.9.0-beta03. I noticed when I use a serializable value class, like this...
Copy code
@Serializable
@JvmInline
value class Id(val value: String)
... as part of a route class like this ...
Copy code
@Serializable
data class Document(val id: Id)
Then I get an error when trying to navigate with it.
Copy code
DocumentListScreen(onDocumentClicked = { id -> nav.navigate(Document(id)) })
Here's what the error looks like:
Copy code
Route Document could not find any NavType for argument id of type Id - typeMap received was {}
I've worked around this by unwrapping the value.
Copy code
DocumentListScreen(onDocumentClicked = { id -> nav.navigate(Document(id.value)) })
And then reinstantiating the
Id
value class on the other side. But I'm wondering whether I'm doing things right. Is there a more appropriate way to make this work? I'd love to keep using value classes for things like this if possible.
l
have you tried defining and passing in the typeMap?
Copy code
composable<Screen.Theme>(typeMap) {
   ThemeScreen(navController)
}

private val typeMap = mapOf(
    typeOf<Uuid>() to serializableNavType(Uuid.serializer()),
)

//example with inline fun <reified T> serializableNavType...
although I would have hoped that the value class would get inlined to String and everything would work by default
d
Thanks @Lukáš Kúšik - the typeMap is what I needed! Maybe we'll get some automatic support for value classes with this in the future, but this has me squared away for now. And it's much better than the manual wrapping/unwrapping.