Hey guys when using compose, viewmodel, and hilt...
# android
v
Hey guys when using compose, viewmodel, and hilt. What would be the best way to pass an argument from the viewmodel to a repository? On a click on list the ID of item is passed to the details viewmodel. But in my repository I need to use that ID to retrieve the recipe
I guess I could call the recipe by its id from firestore directly from the viewmodel (to make it work) but I would like to call the recipe from the repository using flow. I feel like there has to be a way using hilt? But I could be wrong.
s
The repository should be a dependecy of the viewmodel. And after you receive the ID via a click event you pass it to the repository
v
Ok, so I was on the right track. I have the repository as a dependency already, how do I go about passing it to the repository? Do I have a variable in the repository and a function that sets the variable? Like repo.setId(recipeId)?
s
Since repositories are usually singletons that wouldn’t be the best approach. You should define a function inside the repo that returns what you need:
fun returnMyData(id: Long): DataDetails
Besides this approach, you can also define a Flow or some other reactive way of getting your data
✔️ 1