I am finally starting with Navigation but I am stu...
# compose
n
I am finally starting with Navigation but I am stuck with a basic problem:
Copy code
Scaffold(
            scaffoldState = scaffoldState,
            drawerContent = {
                HomePageDrawer()
            },
For HomePageDrawer() I get : "No value passed for parameter 'navController'". What am I supposed to put for parameter navController? Whatever I try gives me a nullpointer... Basically I have some items in my drawer and I want to navigate to other screens from them...
i
The Getting Started part of the guide (https://developer.android.com/jetpack/compose/navigation#getting-started) states that you should be using
rememberNavController()
outside of your
Scaffold
I.e., in a place where every Composable that needs to access it can access it
n
I'm using it in my navigation composable:
Copy code
@Composable
fun ApplicationNavigation() {
    val navController = rememberNavController()
    androidx.navigation.compose.NavHost(
        navController = navController,
        startDestination = "homepage"
    ) {
        composable("homepage") {
            HomePage()
        }
        composable("drawer"){
            HomePageDrawer(navController)
        }
        composable("placeholder"){
            PlaceHolder()
        }
    }
But how can I refer to it in my HomePage Scaffold? I'm a bit confused.
Hmm, I'll try to do what you suggested and as the doc indicates, thanks.
i
Just pass in
navController
👍 1
n
You could also consider passing a lambda instead of passing the
navController
, that way you can keep all navigation related code in one place and not pass the
navController
around
n
@Nikola Drljaca Thanks for this, I'm interested in your idea. Would you be able to show me how it would look, if you have the time? Maybe with my example above?
n
Well, here is a quick example of something thats similar to what I use. For your example, I think you can pass the lambda down your composable tree to wherever you need to call
navigate()
. Maybe the parent of the scaffold can have a lambda and pass it to the
HomePageDrawer()
. Anyway, here's a gist https://gist.github.com/nikolaDrljaca/06013137c325638ac6b71ca3ac76c681
Of course, this might not be the "proper" way to do it, but to me it seemed cleaner than passing the
navController
everywhere.
n
Thanks for this, really appreciate it
i
Yep, that's actually the recommended practice to make your composables separately testable (from a bit farther down that exact same page I linked earlier): https://developer.android.com/jetpack/compose/navigation#testing
n
Oh excellent, thanks, I really need to start reading the doc more carefully! 😉