Search code examples
androidandroid-jetpack-navigationandroid-jetpack-compose

No method navigate(String) in compose-navigation


I'm trying the new jetpack library "compose-navigation".

According to the docs, to navigate to a route, I should use navigate() method which takes a String.

navController = rememberNavController()

// navigate
navController.navigate("/another_route")

However, no such method exists navigate(String) and I get compile error.

What am I missing?


Solution

  • navigate(String) is not part of NavController class, but an extension function

    To solve the error, "import the function route in the file":

    import androidx.navigation.compose.navigate
    
    // and then navigate
    navController.navigate("/another_route")
    

    And it'll work fine.

    Unfortunately, you don't get the completion that you expect.