kotlin Android Jetpack导航可选参数正在返回默认值

x33g5p2x  于 2023-03-19  发布在  Kotlin
关注(0)|答案(1)|浏览(153)

我有一个带有2个屏幕的项目,主屏幕显示配方列表,详细信息屏幕显示用户点击的配方的更多详细信息。我遇到的问题是,我附加到DetailScreen路径的recipeID没有返回到savedStateHandle中,而是使用默认值0。
Here is a link to my project on my github问题可以在应用程序-〉源代码-〉主界面-〉java/com/googleplaystore/spoonfed -〉演示文稿-〉导航-〉详细信息NavGraph中找到
This is the detailnavGraph it shows the navigation call where the recipeId is attached to the route and also the composable
This is my AppNavHost, on line 25 you can see that I use the navigateToDetail function and I pass the recipeId to it.
This the recipeItem composable that gets the recipeId from the state and sets it val recipeId, recipeId is then passed up through other composable until it reaches the AppNavHost
This is my viewmodel, here you can see the saveStateHandle value on line 35 and also my api call function that takes the recipeId on line 48
希望这些图片有助于更好地理解我的问题。

2eafrhcq

2eafrhcq1#

感谢您提供详细的解释。我已经编译了您的项目并发现了问题。问题出在navigateToDetail查询的解析中。它应该读取detail_screen?recipeId=3而不是detail_screen?recipeId={3}
要解决此问题,请删除花括号并更新代码行,如下所示:
替换:

this.navigate("${Screens.DetailScreen.route}?recipeId={$recipeId}")

其中:

this.navigate("${Screens.DetailScreen.route}?recipeId=$recipeId")

相关问题