get As Flow
Creates a Flow that emits the element at the specified index whenever the list changes.
This extension function allows you to observe changes to a specific position in the reactive list without having to observe the entire list. The flow only emits when the element at the specified index actually changes.
If the index is out of bounds, it emits null. This is useful for observing a specific position in the list without causing an exception if the list shrinks or the index becomes invalid.
Example:
val fruits = reactiveListOf("Apple", "Banana", "Cherry")
// Observe the element at index 1
fruits.getAsFlow(1).collect { fruit ->
println("Fruit at index 1: $fruit")
} // Immediately emits: "Fruit at index 1: Banana"
// Observe an out-of-bounds index
fruits.getAsFlow(5).collect { fruit ->
println("Fruit at index 5: $fruit")
} // Immediately emits: "Fruit at index 5: null"
// Modify the list
fruits[1] = "Blueberry" // Emits: "Fruit at index 1: Blueberry"
fruits.add("Dragonfruit") // No emission for index 1 flow
fruits.removeAt(0) // Emits: "Fruit at index 1: Cherry" (was at index 2)
fruits.clear() // Emits: "Fruit at index 1: null"Content copied to clipboard
Return
A Flow that emits the element at the given index, or null if the index is invalid.
Parameters
E
The type of elements in the list.
index
The index of the element to observe.