Thread
#compose
    k

    knthmn

    1 year ago
    How do you encode a string as an destination argument for
    NavHostController.navigate()
    ? My database is backed by a
    String
    primary key that can contain chars like
    ?
    and
    /
    .
    i

    Ian Lake

    1 year ago
    Uri.encode
    Navigation will automatically decode for you though
    k

    knthmn

    1 year ago
    Is it fine to use this even if my key does not confirm to the
    Uri
    standard? edit: nvm it is the static method that takes a
    String
    Nick

    Nick

    3 months ago
    Reviving this thread: If I encode "Nick Achtien (age 4)", then get the item from the back stack, it comes back as "Nick+Achtien+(age+4)" . I'm encoding because I want to make sure that this text doesn't contain any characters like "/"
    i

    Ian Lake

    3 months ago
    Navigation will automatically
    Uri.decode
    the string values before putting them in your
    arguments
    . It sounds like something is causing you to double encode
    Nick

    Nick

    3 months ago
    object LabResultsList : HistoryRoutes("labresults/{patientId}/{patientTitle}/{isDependent}") {
            fun createRoute(patientTitle: String, isDependent: Boolean, patientId: String): String {
                val encodedPatientId = patientId.encode()
                val encodedPatientTitle = patientTitle.encode()
    
                return "labresults/$encodedPatientId/$encodedPatientTitle/$isDependent"
            }
        }
    
    composable(route = HistoryRoutes.LabResultsList.route) { backStackEntry ->
        val patientTitle = backStackEntry.arguments?.getString("patientTitle")!!
        val patientId = backStackEntry.arguments?.getString("patientId")!!
        val isDependent = backStackEntry.arguments?.getBoolean("isDependent")!!
        LabResultsListScreen(
            navController = navController,
            patientId = patientId,
            patientTitle = patientTitle,
            isDependent = isDependent
        )
    }
    
    private fun String.encode() = URLEncoder.encode(this, UTF_8.name())
    oh maybe it's because i'm using URLEncoder?
    hmm
    yes that was it!
    Don't use
    URLEncoder
    , you have to use
    Uri.encode
    i

    Ian Lake

    3 months ago
    Glad you were able to figure it out!