I am just learning StateFlow. I have a simple StateFlow to observe:
private val _userClickedAction = MutableStateFlow( Event(false))
val userClickedAction: StateFlow<Event<Boolean>> = _userClickedAction.stateIn(
scope = viewModelScope,
started = WhileSubscribed(5000),
initialValue = Event(false)
)
I'm trying to observe it in an activity like this:
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
valuePropViewModel.userClickedAction.collect {
valuePropViewModel.actionContext.runActionNamed(SECOND_OPTION_SELECTED)
}
}
}
It won't compile, I am getting the compile error:
Type mismatch. Required: FlowCollector<Event<Boolean>> Found: () → Unit
I'm using:
// Lifecycle components
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-process:$lifecycle_version"
Where $lifecycle_version = 2.4.0