How do you encode a string as an destination argum...
# compose
k
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
Uri.encode
Navigation will automatically decode for you though
🙏 2
k
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
n
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
Navigation will automatically
Uri.decode
the string values before putting them in your
arguments
. It sounds like something is causing you to double encode
n
Copy code
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?
🤷 1
hmm
yes that was it!
Don't use
URLEncoder
, you have to use
Uri.encode
i
Glad you were able to figure it out!
👍 1