Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation destination that matches request NavDeepLinkRequest cannot be found in the navigation graph NavGraph

I have a NavGraph that looks like this:

@Composable
fun NavGraph (
    navController: NavHostController
) {
    NavHost(
        navController = navController,
        startDestination = "Products"
    ) {
        composable(
            route = "Products"
        ) {
            ProductsScreen(
                navController = navController
            )
        }
        composable(
            route = "Product Details",
            arguments = listOf(
                navArgument("product") {
                    type = NavType.SerializableType(Product::class.java)
                }
            )
        ) {
            val product = navController.previousBackStackEntry?.arguments?.getSerializable("product") as Product
            ProductDetailsScreen(
                navController = navController,
                product = product
            )
        }
    }
}

Inside the ProductDetailsScreen, I want on product click to navigate further to details screen passing a Product object:

LazyColumn {
    items(
        items = products
    ) { product ->
        ProductCard(
            product = product,
            onProductClick = {
                navController.currentBackStackEntry?.arguments?.putSerializable("product", product)
                navController.navigate("Product Details")
            }
        )
    }
}

The products are perfectly displayed but when I click on a product, the app crashes with this error:

java.lang.IllegalArgumentException: Navigation destination that matches request NavDeepLinkRequest{ uri=android-app://androidx.navigation/Product Details } cannot be found in the navigation graph NavGraph(0x0) startDestination={Destination(0xb543811) route=Products}

Can anyone help?

P.S. I also followed this answer but no luck :(

like image 445
Joan P. Avatar asked Sep 12 '25 17:09

Joan P.


2 Answers

I had this error when i was using json as string to pass class to my navigation here is how i implemented :

 composable(
            "details/{MyObject}",
            arguments = listOf(navArgument("MyObject") {
                type = NavType.StringType
            })
        ) {
            it.arguments?.getString("MyObject")?.let { jsonString ->
                var issue = MyObject()
                if (!jsonString.isNullOrEmpty())
                    issue = jsonString.fromJson(MyObject::class.java)
                DetailsView(navController = navController, detailsViewModel, myObject = MyObject)
            }
        }

And how i call it :

val gson = Gson()
var myObjectString = gson.toJson(myObject, MyObject::class.java)
navController.navigate("details/$myObjectString")

And here sometime i was getting the Navigation destination that matches request NavDeepLinkRequest cannot be found in the navigation graph NavGraph error and sometimes i didn't got it. it was depending on the object i was clicking.

I think it's because some of my data was not well formed when i got the answer from the api meaning i might have some unwanted characters when i was transforming it to json. To avoid that i encoded my json in utf8 like so :

var encode = URLEncoder.encode(myObjectString,StandardCharsets.UTF_8.toString())
navController.navigate("details/$encode")

Then i didn't got the navigation error again, i hope that could help people that are trying to use jetpack compose navigation with json to send object

like image 196
Aqua Freshka Avatar answered Sep 15 '25 06:09

Aqua Freshka


Make sure you have all the argument names and route names correctly setup and avoid spaces, use underscores when you want to break words

My Mistake

The argument name in the route definition and while supplying in the navArgument block should be same

composable(
                        "pokemon_detail_screen/{dominantColor}/{pokemonName}", //Notice over here
                        arguments = listOf(
                            navArgument("dominantColor") {
                                type = NavType.IntType
                            },
                            navArgument("pokemon_name") { // Notice over here
                                type = NavType.StringType
                            }
                        )
                    )

Took me quite some time to figure it out so be very careful

like image 38
gtxtreme Avatar answered Sep 15 '25 05:09

gtxtreme