theapache64

    theapache64

    1 year ago
    🧭 Navigation: when calling
    arguments?.getString
    ,which
    userId
    should be used? red or yellow? and what abt
    navArgument
    ? 🤔
    Filip Wiesner

    Filip Wiesner

    1 year ago
    I would assume yellow because that's the parameter name 😄
    And the red is for
    navArgument()
    I guess
    theapache64

    theapache64

    1 year ago
    Oookay 😄 I’ll try that
    Filip Wiesner

    Filip Wiesner

    1 year ago
    Let me know when you find out 😅
    theapache64

    theapache64

    1 year ago
    @Ian Lake I think it’d be great if you guys can use different names here. Its little confusing
    @Filip Wiesner sure
    JulianK

    JulianK

    1 year ago
    I'm using path arguments like this:
    route = "profile/{userId}"
    navArgument("userId")
    params.backStackEntry.arguments?.getLong("userId")
    So in your case... red is the the thing to query for, i guess?
    theapache64

    theapache64

    1 year ago
    @JulianK yeah pathArgs doesn’t make any confusion. but optional arg example kinda confusing. didn’t try it though.
    Filip Wiesner

    Filip Wiesner

    1 year ago
    Did you test the yellow/red parameters @theapache64? I am kinda curious how it actually is 😄
    theapache64

    theapache64

    1 year ago
    @Filip Wiesner 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
    via theKey : null
    via theValue : myValue
    😄
    Filip Wiesner

    Filip Wiesner

    1 year ago
    😞 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 🙃
    theapache64

    theapache64

    1 year ago
    Another interesting output with the same
    navigate
    call as above 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
    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
    Filip Wiesner

    Filip Wiesner

    1 year ago
    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
    theapache64

    theapache64

    1 year ago
    Yeah. I agree.