android-jetpack-composeandroid-jetpack-navigation

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


Here is my set up:

@Composable
fun MyNavHost(navController: NavHostController = rememberNavController(), padding: PaddingValues) {
    val actions = remember(navController) { MainActions(navController) }

    NavHost(
        navController = navController,
        startDestination = TopLevelDestination.Home.route, modifier = Modifier.padding(padding)
    ) {
        composable(TopLevelDestination.Home.route) {
            HomeScreen(onNavigateToArticle = actions.navigateToArticle)
        }
        composable(
            "${MyRoute.ARTICLE_ROUTE}/${MyRoute.ARTICLE_ID}",
            arguments = listOf(navArgument(MyRoute.DATE_KEY) { type = NavType.LongType })
        ) { backStackEntry ->
            ArticleScreen(
                articleId = backStackEntry.arguments?.getLong(MyRoute.ARTICLE_ID)!!
            )
        }
      }
    }

    /**
     * Models the navigation actions in the app.
     */
    class MainActions(navController: NavHostController) {
        val navigateToArticle: (Long) -> Unit = { articleId: Long ->
            navController.navigate("${MyRoute.ARTICLE_ROUTE}/${articleId}")
        }
    }

Here is the code for the Article Item from a lazy column which is clickable

 Row(
    modifier = modifier
        .clickable { onNavigateToArticle(articleId) }
        .padding(10.dp)
        .heightIn(72.dp),
    horizontalArrangement = Arrangement.SpaceBetween,
    verticalAlignment = Alignment.CenterVertically
) 

I get the following exception:

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

I read this article and it seemed pretty straightforward. I can't really figure out what I am doing wrong. I read some answers on StackOverflow already, unfortunately they don't apply to my case.


Solution

  • Instead of:

    "${MyRoute.ARTICLE_ROUTE}/${MyRoute.ARTICLE_ID}"
    

    I just had to write

    "${MyRoute.ARTICLE_ROUTE}/{${MyRoute.ARTICLE_ID}}"
                              ^                     ^
                              |                     |
                                extra brackets here
    

    So one pair more of bracket (around the argument).