is there any better way to write this code?
code in thread
Mjahangiry75
05/30/2021, 9:47 AM
Copy code
private val documentId = MutableStateFlow<String?>(null)
private val _document = MutableStateFlow<Document?>(null)
val document: StateFlow<Document?> = _document
init {
viewModelScope.launch {
documentId.collect {
_document.value = repository.findById(it!!).first()
}
}
}
I tried it but seems something is wrong
Copy code
private val documentId = MutableStateFlow<String?>(null)
val document: StateFlow<Document?> = documentId.filterNotNull().transform {
repository.findById(it)
}.stateIn(viewModelScope, SharingStarted.Lazily, null)
d
Daniel
05/30/2021, 11:51 AM
Why is documentId a stateFlow? Can it just be passed as an argument to a function.
Copy code
private val documentId = MutableStateFlow<String?>(null)
val document: StateFlow<Document?> = documentId.filterNotNull().flatMap { id ->
repository.findById(id)
}.asStateFlow()