value As Flow
Creates a Flow that emits the value associated with the specified key whenever this map changes.
This extension function allows you to observe changes to a specific key in the reactive map without having to observe the entire map. The flow only emits when the value for the specified key actually changes.
If the key is not present in the map, it emits null. When the key is removed from the map, it will emit null as well.
Example:
val userMap = reactiveMapOf("name" to "Alice", "age" to "25", "city" to "NYC")
// Observe changes to the "name" key specifically
userMap.valueAsFlow("name").collect { name ->
println("Name is now: $name")
} // Immediately emits: "Name is now: Alice"
// Observe a key that doesn't exist
userMap.valueAsFlow("email").collect { email ->
println("Email is: $email")
} // Immediately emits: "Email is: null"
// Modify the map
userMap["name"] = "Bob" // Emits: "Name is now: Bob"
userMap["age"] = "30" // No emission for name flow
userMap["email"] = "bob@test" // Emits: "Email is: bob@test"
userMap.remove("name") // Emits: "Name is now: null"Content copied to clipboard
Return
A Flow that emits the value for the given key, or null if the key is absent.
Parameters
K
The type of keys in the map.
V
The type of values in the map.
key
The key whose associated value is to be observed.