Hey, is there any easy way to create bundle from t...
# compose-destinations
r
Hey, is there any easy way to create bundle from the navargs data class? we can create args from destination ex GeneratedScreenDestination.argsFrom() but there is nothing like ScreenArgs(arg,arg).toBundle()
d
Looking at the generated code I don't think anything exists today. I guess you could go through the SavedStateHandle and then create the bundle (I've not tested it):
Copy code
private fun SavedStateHandle.toBundle() = keys()
    .map { it to get<Any>(it) }
    .let { bundleOf(pairs = it.toTypedArray()) }
// or
private fun SavedStateHandle.toBundle() = keys().associateWith { get<Any>(it) }.toPersistableBundle()

// To create
val bundle = YourNavArgs("param1", "param2").toSavedStateHandle().toBundle()
// To parse
val args: YourNavArgs = YourDestination.argsFrom(bundle)
https://composedestinations.rafaelcosta.xyz/v2/testing?_highlight=savedst#savedstatehandle-from-nav-arguments
🙌 1
r
nice! i was just trying to make some parser but never thought of using SavedStateHandle first, that seems to be working perfectly so far. I was in need of having something that could convert args to bundle due to still mixed up navcomponent with compose saved me a lot of headache, thank you!
👍 1
d
No problem 🙂
r
Yes this was the type of solution that came to mind as well. Let me know if you find any limitations or issues with it 🙂
👍 2
r
@Rafael Costa I happen to catch
Copy code
java.lang.NoSuchMethodError: No static method toSavedStateHandle
from generated files for some reason. Fails runtime
r
Is this on release build with R8? 🤔
r
No, debug build I can clearly see generated methods and files though
r
It’s very weird.. I don’t know what else could cause this 🤔
r
Had one more view on that and it seems I was missing one ksp enclosure within module configuration, it seems like it overwrites files but still it is able to compile. Instead of throwing some errors.