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

Viewed 4621

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 :(

3 Answers

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

It is my sample

@ExperimentalPagerApi
@Composable
fun WallNavigation(nameFolder: String, paths: List<String>) {
   val navController = rememberNavController()
   val model: ImageViewModel = viewModel()
   model.setFolder(nameFolder)
   model.setFileNames(paths)

   NavHost(navController = navController, startDestination = Screen.MainScreen.route) {
    composable(Screen.MainScreen.route) {
        MainScreen(navController = navController, model)
    }

    composable(route = Screen.GalleryScreen.route + "/{position}", arguments = listOf(navArgument(name = "position") {
        type = NavType.IntType
        defaultValue = -1
        nullable = false
    })) { entity ->
        GalleryScreen(navController = navController, position = entity.arguments?.getInt("position") ?: -1, imageViewModel = model)
    }

    composable(route = Screen.CropScreen.route) {
        CropScreen(navController = navController, imageViewModel = model)
    }
  }
} 

sealed class Screen(val route : String) {
    object MainScreen : Screen("main_screen")
    object GalleryScreen : Screen("gallery_screen")
    object CropScreen : Screen("crop_screen")

    fun withArgs(vararg args: String):String {
       return buildString {
          append(route)
          args.forEach { arg ->
              append("/$arg")
          }
        }
     }
}

This answer explains really good how to pass a parcelable argument with compose navigation.

The error message you describe above that the navigation destination that matches request NavDeepLinkRequest cannot be found in the navigation graph NavGraph can also appear when you want to hand over an optional argument and forget to make it nullable.

  arguments: List<NamedNavArgument> = listOf(
            navArgument("product") {
                type = Product.NavigationType //check out the linked answer to know how to write the Product.NavigationType
                nullable = true
            }
        )
Related