Mjahangiry75
05/30/2021, 9:46 AMMjahangiry75
05/30/2021, 9:47 AMprivate 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
private val documentId = MutableStateFlow<String?>(null)
val document: StateFlow<Document?> = documentId.filterNotNull().transform {
repository.findById(it)
}.stateIn(viewModelScope, SharingStarted.Lazily, null)
Daniel
05/30/2021, 11:51 AMprivate val documentId = MutableStateFlow<String?>(null)
val document: StateFlow<Document?> = documentId.filterNotNull().flatMap { id ->
repository.findById(id)
}.asStateFlow()
ursus
05/30/2021, 2:50 PMscope.launch {
id.flatMapLatest {
if (it == null) {
flowOf(null)
} else {
repo.findById(it}
}
}.collect {
_document.value = it
}
}