我在这里和这里检查了答案,将一个对象从一个可组合对象传递到另一个对象,并将一个@Serializable
对象作为字符串从一个可组合对象传递到另一个对象,但得到了结果错误
java.lang.IllegalArgumentException: Navigation destination that matches request NavDeepLinkRequest{ uri=android-app://androidx.navigation/detail/{"id":3,"email":"emma.wong@reqres.in","first_name":"Emma","last_name":"Wong","avatar":"https://reqres.in/img/faces/3-image.jpg"} } cannot be found in the navigation graph NavGraph(0x0) startDestination={Destination(0x60276fc4) route=start_destination}
at androidx.navigation.NavController.navigate(NavController.kt:1530)
数据类
@Serializable
data class User (
val id : Int,
val email : String,
val first_name : String,
val last_name : String,
val avatar : String
)
它是reqres.in api的示例,NavHost
在可组合物之间导航是
NavHost(
navController = navController,
startDestination = "start_destination",
modifier = Modifier.padding(paddingValues)
) {
composable(route = "start_destination") {
ListScreen() { user ->
val json: String = Json.encodeToString(user)
navController.navigate("detail/$json")
}
}
composable(route = "detail/{user}", arguments = listOf(
navArgument("user") {
type = NavType.StringType
}
)) { backStackEntry ->
val arguments = requireNotNull(backStackEntry.arguments)
val user = Json.decodeFromString<User>(string = arguments.getString("user")!!)
DetailScreen(user = user)
}
}
navController.navigate("detail/$json")
调用时发生崩溃。