I am trying to show items from my room database with JetPack Compose like in this example.
My Entity:
@Entity(tableName = "trains")
data class TrainSearch(
@PrimaryKey(autoGenerate = true)
override var id: Long = 0L,
var type: TrainType = TrainType.ICE,
var number: String = "",
override var name: String? = null
) : Listable {}
My Repository:
class ListableRepositoryImpl(private val trainSearchDao: TrainSearchDao) : ListableRepository {
override fun fetchAllItems(): Flow<TrainSearch> {
return trainSearchDao.getAllTrainSearches()
}
}
My ViewModel:
class FavListViewModel(private val homeRepo: ListableRepository) : ViewModel() {
val savedItems = homeRepo.fetchAllItems()
}
My Compose Function:
(...)
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
(...)
fun FavListScreen(
modifier: Modifier = Modifier,
favListViewModel: FavListViewModel = koinViewModel()
) {
val savedItems:List<TrainSearch> by favListViewModel.savedItems.collectAsState(initial = emptyList<TrainSearch>())
(...)
But I am getting the Error Message, that getValue
is not suitable. And I have no Idea why. :(
Can anyone point me into the right direction?
I figured it out after a lot of debugging and logging sessions. I was using the wrong type Flow<TrainSearch>
. In the example the Author uses typealias Books = List<Book>
. This was misleading me. I had to use Flow<List<TrainSearch>>
.