*Hot to inject class to my viewModel in @Composabl...
# compose
i
Hot to inject class to my viewModel in @Composable? I use Koin and I tried to follow documentation but it doesn't work Module:
Copy code
val welcomeModule = module {
   single<MyService> { MyServiceImpl() }
}
ViewModel:
Copy code
class MyViewModel(private val myService: MyService) : ViewModel() {
...
}
@Composable function:
Copy code
@Composable
fun Screen(){
   val myViewModel: MyViewModel= viewModel()
}
I tried:
Copy code
val myService = get<MyService>()
and put it to viewModel() but it doesn't work
t
are you using hilt?
If not you should try it
z
Use Hilt. No reason to use Koin nowadays
a
You need to add
viewModel<MyViewModel> { MyViewModel(get()) }
into your welcome module. essentially your missing the binding that tells Koin how to create an instance of MyViewModel.
i
@Abhishek Dewan Why I try to do it I get error: Cannot create an instance of class MyViewModel
a
Did you also replace your getting of the viewModel with something like
val viewModel: MyViewModel = get()
and be sure to use the get() from koin