Observe As Events
Observes a Flow and executes the onEvent callback for each emitted value.
This composable function is designed to handle events from a Flow in a lifecycle-aware manner. It uses LaunchedEffect and repeatOnLifecycle to ensure that the flow is collected only when the composable is in the STARTED state and is automatically cancelled when the composable is destroyed.
This is particularly useful for scenarios where you want to trigger side effects (e.g., showing a Snackbar, navigating to another screen) based on events emitted by a ViewModel or another business logic component.
Parameters
The Flow to observe.
An optional key that, if changed, will cause the LaunchedEffect to be cancelled and restarted. This can be useful if the observation logic depends on this key.
Another optional key that, if changed, will cause the LaunchedEffect to be cancelled and restarted.
A lambda function that will be invoked with each value emitted by the flow. This function is executed on the main thread.
Type Parameters
The type of data emitted by the Flow.