Is there any way to construct `NavArgs` on the fly...
# koin
i
Is there any way to construct
NavArgs
on the fly and inject then directly into
ViewModel
?
t
NavArgs
require access to the
arguments
, thus you have to create these within the
Fragment
.
Copy code
val args by navArgs<MyScreenArgs>()
val viewModel by viewModel<MyScreenViewModel> {
    parametersOf(args)
}
This is the best I came up with, but it's sufficient for my needs.
i
So how would you retrieve these args within MyScreenViewModel?
t
The parameters are provided within your Koin factory as a parameter:
Copy code
viewModel { MyScreenViewModel(it.get()) }
a
you can use the destructured declaration here in the definition, for better reading
Copy code
viewModel { (navArgs : MyScreenNavArgs) -> MyScreenViewModel(navArgs) }
t
I prefer to not use this. Firstly it needs a declared type, secondly it requires to know the correct order when more than one parameter is available.
it.get()
mirrors the
get()
nicely and doesn't require to know the order of the parameters.
a
interesting feedback 👍
I always written them like that, but you raise some good points