https://kotlinlang.org logo
n

Nat Strangerweather

01/30/2021, 7:21 PM
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

Ian Lake

01/30/2021, 7:50 PM
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

Nat Strangerweather

01/30/2021, 7:58 PM
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

Ian Lake

01/30/2021, 8:22 PM
Just pass in
navController
👍 1
n

Nikola Drljaca

01/31/2021, 11:13 AM
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

Nat Strangerweather

01/31/2021, 1:29 PM
@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

Nikola Drljaca

01/31/2021, 2:42 PM
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

Nat Strangerweather

01/31/2021, 3:29 PM
Thanks for this, really appreciate it
i

Ian Lake

01/31/2021, 4:07 PM
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

Nat Strangerweather

01/31/2021, 4:14 PM
Oh excellent, thanks, I really need to start reading the doc more carefully! 😉
2 Views