In my task I need to fetch list of bikes.
I made BikesApi and put some bikes into a list. In MovieRepository implementation I overrided function getBikes that returns now Flow<List>>.
val BikeZ : Flow<List<Bike>> = flow{
while(true){
val lastBikeX = bikeApi.getBikes()
emit(lastBikeX)
kotlinx.coroutines.delay(1000)
}
}
return BikeZ
In BikeViewModel I implemented it like this, It gets MovieRepository (Koin):
public fun getPopularBikes() = flow<Bike> {
val bikesList = repository.getPopularBikes().collect()
return@flow bikesList
}
And when I try in Compose function this:
val viewModel = viewModel<BikeViewModel>()
I got this error: Cannot create an instance of class BikeViewModel.
java.lang.RuntimeException:Cannot create an instance of class BikeViewModel
I'm using Koin. I need list of Movies to implement them into UI.
EDIT: In app module i did this:
single<BikeApi>{
BikeApiImpl()
}
single<BikeRepository>{
BikeRepositoryImpl(get())
}
viewModel { BikeViewModel(get()) }
Bike api:
class BikeApiImpl : BikeApi{
override suspend fun getPopularBikes(): List<Bike> {
return listOf(
Bike(
id = 1,
name = "Nakamura",
isCheckedOff = true,
bikeType = "MTB",
overview = "None",
picture = R.drawable.nakamura_1,
userScore = 72.0
)
I't wont return list of Bikes.
@HiltViewModelannotation, as well as@Injectannotation for your constructor. If you don't use Hilt, how do you expect your repo to be injected?