:wave: I use Compose Multiplatform Navigation v 2....
# compose
h
đź‘‹ I use Compose Multiplatform Navigation v 2.8.0-alpha10. My app crashes in runtime due to:
Route …CharacterDetail could not find any NavType for argument id of type …Character.Id - typeMap received was {}
For better perspective see the code:
Copy code
@Serializable
public data class CharacterDetail(val id: Character.Id) 

composable<CharacterDetail> { backStackEntry ->
    val character: CharacterDetail = backStackEntry.toRoute()
    CharacterDetailScreen(character.id, onNavigateBack)
}

public data class Character(id: Character.Id, ...) {
    @Serializable
    public data class Id(val value: Int)
}
Is the @Serializable type safe navigation argument still not supported in CMP?
i
As the error message says, you didn't tell Navigation how to represent your
Character.Id
class. You need to do that if any of the fields in your class aren't primitive types
FWIW, value classes (which you don't seem to be using here, even though it looks like the perfect use case) are supported in the latest point release of the AndroidX artifacts (no idea if there is a CMP version built against it though): https://developer.android.com/jetpack/androidx/releases/navigation#2.8.4
h
Thank you for your answer.
Copy code
@Serializable
public data class Id(val value: Int)
There is only a primitive type inside
Character.Id
class. What I am missing, please? Also, I know a value class would be a perfect match for this use case, but we had some problems using them from the iOS side, so we abandoned them.
i
You need to provide a custom NavType for your class and pass it through the typeMap, like the error says: https://developer.android.com/guide/navigation/design/kotlin-dsl#custom-types
h
Thank you 🙌
g
Does this work with compose multiplatform? I’m using
org.jetbrains.androidx.navigation:navigation-compose:2.9.1
and when I navigate I get
Copy code
java.lang.IllegalArgumentException: Navigation destination that matches route com.test.compose.Search/{"searchQuery":"test","filters":[""]} cannot be found in the navigation graph ComposeNavGraph(0x0) startDestination={Destination(0x56174464) route=com.test.compose.NavigationDestination.Home}
That’s how I registered the route
Copy code
NavHost(
        navController = navController,
        startDestination = NavigationDestination.Home,
        modifier = Modifier.weight(1f)
      ) {
        ...
        composable<Search>(
          typeMap = mapOf(typeOf<SearchParameters>() to SearchParametersType)
        ) { backStackEntry ->
          val searchParameters = backStackEntry.toRoute<Search>().parameters
          Text("Testing: $searchParameters")
        }
     }
If I remove the
typeMap
I get the expected error that it
can't find the NavType for argument parameters of type SearchParameters
, but if I declare the nav type it opens the app, correctly call the the methods on my custom NavType, but crashes saying that it couldn’t find the destination when I try to navigate
i
That is the kind of error you get when you aren't Uri encoding your
serializeAsValue
result
g
Thanks for the help @Ian Lake. I was an issue with my NavType implementation. The
put
method was empty, I had forgotten to add the value to the bundle.
198 Views