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


Solution 1:

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")
          }
        }
     }
}