android 在编写中使用参数导航

lvmkulzt  于 2023-04-04  发布在  Android
关注(0)|答案(1)|浏览(143)

我必须使用参数clientId导航到另一个撰写屏幕。
我的屏幕上有:

object ClientsListScreen : Screen(route = "clients_list_screen")

    object ClientDetailsScreen : Screen(route = "client_details_screen/{clientId}") {
        fun routeWithClientId(clientId: String): String {
            return "client_details_screen/{$clientId}"
        }
    }

设置导航图:

composable(route = "${Screen.ClientDetailsScreen.route}/{clientId}") { backStackEntry ->

            val clientId = backStackEntry.arguments?.getString(CLIENT_ID)

            ClientDetailsScreen(
                navController = navController,
                clientId = clientId!!
            )
        }

客户端项目:

.clickable {
  navController.navigate(Screen.ClientDetailsScreen.routeWithClientId(client.id.toString()))}

和客户详细信息屏幕:

@Composable
fun ClientDetailsScreen(
    navController: NavController,
    clientId: String
) {
   Text(text = "DETAILS, $clientId")
}

我得到这个错误:
java.lang.非法参数异常:在导航图NavGraph(0x 0)startDestination={Destination(0x 7a 02 aac 9)route=clients_list_screen}中找不到与请求NavDeepLinkRequest{ uri=android-app://androidx.navigation/client_details_screen/{ }匹配的导航目标
是什么原因导致了这个错误,ClientListScreen的某些东西?ClientId永远不会为null或-1或其他东西

00jrzges

00jrzges1#

我是这样解决的:
屏幕:

object ClientDetailsScreen : Screen(route = "client_details_screen/{$CLIENT_ID_KEY}"){
      fun passClientId(id: Int): String {
         return this.route.replace(oldValue = "{$CLIENT_ID_KEY}", newValue = id.toString())
        }
    }

设置导航图:

composable(
            route = Screen.ClientDetailsScreen.route,
            arguments = listOf(navArgument(CLIENT_ID_KEY) {
                type = NavType.IntType
            })
        ) {

            ClientDetailsScreen(
                navController = navController, clientId = it.arguments?.getInt(
                    CLIENT_ID_KEY
                )
            )
        }

客户卡项目:

.clickable {
       navController.navigate(route = Screen.ClientDetailsScreen.passClientId(client.id))
            }

相关问题