Reputation: 1
first, sorry for my bad eng.
using this app and
reading this chapter
class ItemDetailsViewModel
has a statement below.
private val itemId: Int = checkNotNull(savedStateHandle[ItemDetailsDestination.itemIdArg])
I have known savedStateHandle must have a setting section like
operator fun set(key: String, value: Any?) { initialState[key] = value }
so savedStateHandle loads the value using the key.
At this point, I couldn't find savedStateHandle
set his value using ItemDetailsDestination.itemIdArg
key in the app.
Asking ChatGPT, savedStateHandle
can't create value automatically, they must directly set the key-value map. But also ChatGPT can't find the setting point.
Am I curious for Being of no use? I can't sleep until I find the answer. Please somebody put me to sleep.
Upvotes: -1
Views: 48
Reputation: 4276
That argument is set via Compose Navigation with arguments.
As you can see in InventoryNavGraph.kt
, ItemDetailsScreen
has one argument in its destination named ItemDetailsDestination.itemIdArg
.
composable(
route = ItemDetailsDestination.routeWithArgs,
arguments = listOf(navArgument(ItemDetailsDestination.itemIdArg) {
type = NavType.IntType
})
) {
ItemDetailsScreen(
//...
The argument is set when user navigates from HomeScreen
using navigateToItemUpdate
composable(route = HomeDestination.route) {
HomeScreen(
navigateToItemEntry = { navController.navigate(ItemEntryDestination.route) },
navigateToItemUpdate = {
navController.navigate("${ItemDetailsDestination.route}/${it}")
}
)
}
Upvotes: 0