Here is a detailed answer for using multiple optional parameters
// Syntax to declare
NavHost(...) {
composable(
"profile?userId={userId}&username={username}&address={address}",
arguments = listOf(
navArgument("userId") {
type = NavType.IntType
defaultValue = 86
}
navArgument("username") {
type = NavType.StringType
defaultValue = "rahul2211"
}
navArgument("address") {
type = NavType.StringType
defaultValue = "India"
}
)
) { backStackEntry ->
val userId = backStackEntry.arguments?.getInt("userId")
val username = backStackEntry.arguments?.getString("username")
val address = backStackEntry.arguments?.getString("address")
Profile(navController, userId, username, address)
}
}
This is how you can optionally pass parameters to the Profile() composable
// Syntax to call
navController.navigate("profile?userId=102&username=shrekssid&address=Germany")
// userId = 102, username = "shrekssid", address = "Germany"
navController.navigate("profile?address=Italy")
// userId = 86, username = "rahul2211", address = "Italy"
navController.navigate("profile?userId=991&address=Germany")
// userId = 991, username = "rahul2211", address = "Germany"
navController.navigate("profile")
// userId = 86, username = "rahul2211", address = "India"