如何在Android Jetpack Compose中传递可选布尔参数?



我试图在仪表板上向用户显示欢迎消息,当他可以从欢迎页面。因此,在Dashboard的路由中,我定义了一个可选参数。

没有任何参数(因为,它是可选的),但是当我添加一个参数时,它不再工作了。

composable(Routes.welcome) {
WelcomeScreen {
navController.navigate("dashboard?isWelcome={true}")
}
}
composable(
"dashboard?isWelcome={isWelcome}}",
arguments = listOf(
navArgument("isWelcome") {
type = NavType.BoolType
}
)
) {
val isFromWelcome = it.arguments?.getBoolean("isWelcome") ?: false
...
}

错误:

Navigation destination that matches request NavDeepLinkRequest{ uri=android-app://androidx.navigation/dashboard?isWelcome={true} } cannot be found in the navigation graph

谢谢你的建议:)

发送布尔值

val booleanValue = true
navController.navigate("my_destination?my_arg_id=${booleanValue}")

收到布尔

composable(
route = "my_destination?my_arg_id=${booleanValue}",
arguments = listOf(navArgument("my_arg_id") { defaultValue = false })
) { from ->
val myBooleanArg = from.arguments?.getBoolean("my_arg_id")
MySecondScreen(myBooleanArg)
}

最新更新