How does Kotlin coroutine Flow .map function know how to apply transformation

184 Views Asked by At

I have this "state" object:

data class StudentState(
   val students: List<Student> = emptyList()
)

In my ViewModel I have val called studentState which I am instantiating this way:

val studentState: StateFlow<StudentState> =
   studentRepository.getStudents().map {
      StudentState(it)
   } .stateIn(
         scope = viewModelScope,
         started = SharingStarted.WhileSubscribed(TIMEOUT_MILLIS),
         initialValue = StudentState()
      )

This code works, but my question is how does the .map function know which value in the StudentState to apply the flow from the repository? What if I want to hold another list in my StudentState for example:

data class StudentState(
   val students: List<Student> = emptyList()
   val studentClasses: List<StudentClasses> = emptyList()
)

If I want to get populate it in the viewmodel from another call tot he repository how would I do it?

I am trying to understand how the .map function works in details in the above example.

0

There are 0 best solutions below