https://kotlinlang.org logo
#compose
Title
# compose
t

theapache64

08/23/2021, 9:48 AM
🧭 Navigation: when calling
arguments?.getString
,which
userId
should be used? red or yellow? and what abt
navArgument
? 🤔
f

Filip Wiesner

08/23/2021, 9:51 AM
I would assume yellow because that's the parameter name 😄
And the red is for
navArgument()
I guess
t

theapache64

08/23/2021, 9:53 AM
Oookay 😄 I’ll try that
f

Filip Wiesner

08/23/2021, 9:54 AM
Let me know when you find out 😅
t

theapache64

08/23/2021, 9:55 AM
@Ian Lake I think it’d be great if you guys can use different names here. Its little confusing
@Filip Wiesner sure
j

JulianK

08/23/2021, 2:36 PM
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

theapache64

08/23/2021, 3:04 PM
@JulianK yeah pathArgs doesn’t make any confusion. but optional arg example kinda confusing. didn’t try it though.
f

Filip Wiesner

08/25/2021, 7:10 AM
Did you test the yellow/red parameters @theapache64? I am kinda curious how it actually is 😄
t

theapache64

08/25/2021, 7:26 AM
@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

Filip Wiesner

08/25/2021, 7:31 AM
😞 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

theapache64

08/25/2021, 7:32 AM
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

Filip Wiesner

08/25/2021, 7:36 AM
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

theapache64

08/25/2021, 7:37 AM
Yeah. I agree.
6 Views