:compass: Navigation: when calling `arguments?.get...
# compose
t
🧭 Navigation: when calling
arguments?.getString
,which
userId
should be used? red or yellow? and what abt
navArgument
? 🤔
f
I would assume yellow because that's the parameter name 😄
And the red is for
navArgument()
I guess
t
Oookay 😄 I’ll try that
f
Let me know when you find out 😅
t
@Ian Lake I think it’d be great if you guys can use different names here. Its little confusing
@Filip Wiesner sure
j
I'm using path arguments like this:
Copy code
route = "profile/{userId}"
navArgument("userId")
params.backStackEntry.arguments?.getLong("userId")
So in your case... red is the the thing to query for, i guess?
t
@JulianK yeah pathArgs doesn’t make any confusion. but optional arg example kinda confusing. didn’t try it though.
f
Did you test the yellow/red parameters @theapache64? I am kinda curious how it actually is 😄
t
@Filip Wiesner Code
Copy code
composable("A"){
    Button(onClick = {
        mainNavController.navigate("B?theKey=myValue")
    }) {
        Text(text = "GO TO B")
    }
}
composable("B?theKey={theValue}"){
    println("via theKey : ${it.arguments?.getString("theKey")}")
    println("via theValue : ${it.arguments?.getString("theValue")}")
}
Output
Copy code
via theKey : null
via theValue : myValue
😄
f
😞 So it was the red arrow. It's weird that when getting an argument from url it does not use the parameter name. But I think it kinda makes sense because it takes the whole url as path and extracts only the specified "arguments" in curly braces. So it does not and does make sense at the same time 🙃
😄 1
t
Another interesting output with the same
navigate
call as above Code
Copy code
composable("B?theKey={theValue}", arguments = listOf(
    navArgument("theKey") { defaultValue = "theDefaultValue" }
)) {
    println("via theKey : ${it.arguments?.getString("theKey")}")
    println("via theValue : ${it.arguments?.getString("theValue")}")
}
Output
Copy code
via theKey : theDefaultValue
via theValue : myValue
It makes sense when we look closely at the code 😄 We asked nav to return
theDefaultValue
when
theKey
is null. Since the
argument.getString
looks for the “placeholder value”,
theyKey
is null and returned its default value ie
theDefaultValue
f
Yeah, that makes sense. But I think the argument just shouldn't have name at all and it should be like this:
B?theKey={}
. It would be less confusing
t
Yeah. I agree.