Using List Store
KStore provides you with some convenient extensions to manage stores that contain lists.
Create a list store
listStoreOf
is the same as storeOf
, but defaults to empty list instead of null
val listStore: KStore<List<Pet>> = listStoreOf(file = "path/to/file")
Get values
val pets: List<Cat> = listStore.getOrEmpty()
val pet: Cat = store.get(0)
or observe values
val pets: Flow<List<Cat>> = listStore.updatesOrEmpty
Add or remove elements
listStore.plus(cat)
listStore.minus(cat)
Map elements
listStore.map { cat -> cat.copy(cat.age = cat.age + 1) }
listStore.mapIndexed { index, cat -> cat.copy(cat.age = index) }
Last modified: 06 November 2024