unable to collect Stateflow in Lifecycle aware manner in onCreate function of a fragment
I have a stateflow which is collected from the viewmodel
private val _someFlow = MutableStateFlow<List<Students>>(emptyList())
val getSomeFlow = _someFlow.stateIn(initialValue = emptyList(),
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5000)
)
init {
getAllStudents()
}
fun getAllStudents() = viewModelScope.launch {
studentRepository.getAllStudents()
.catch { e ->
Log.d(
"main",
"Exception: ${e.message} "
)
}.collect {
_someFlow.value = it
}
}
I am trying to collect it in the fragment
override fun onViewCreated(
view: View,
savedInstanceState: Bundle?
) {
super.onViewCreated(
view,
savedInstanceState
)
viewLifecycleOwner.lifecycleScope.launch {
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
launch {
studentViewModel.getSomeFlow.filter { data ->
data.isNotEmpty()
}.collectLatest { data -> studentList = data }
}
}
}
override fun onStart() {
super.onStart()
if (Build.VERSION.SDK_INT > 23) {
for (student in studentList) {
}
}
}
How ever the list is not populated
when we use lifecycle.repeatOnLifecycle(Lifecycle.State.CREATED)
it works , but in most of the examples it is mentioned to use Lifecycle.State.CREATED
so the question is what should we use the collect the stateflow in proper lifecycle aware manner