Mehdi.ncb
Mehdi.ncb

Reputation: 352

Problem with state in jetpackCompose and Flow

I have a problem for now in JetpackCompose. The problem is, when I'm collecting the Data from a flow, the value is getting fetched from firebase like there is a listener and the data's changing everytime. But tthat's not that.

I don't know what is the real problem!

FirebaseSrcNav

suspend fun getName(uid: String): Flow<Resource.Success<Any?>> = flow {
    val query = userCollection.document(uid)
    val snapshot = query.get().await().get("username")
    emit(Resource.success(snapshot))
}

NavRepository

suspend fun getName(uid: String) = firebase.getName(uid)

HomeViewModel

fun getName(uid: String): MutableStateFlow<Any?> {
    val name = MutableStateFlow<Any?>(null)
    viewModelScope.launch {
        navRepository.getName(uid).collect { nameState ->
            when (nameState) {
                is Resource.Success -> {
                    name.value = nameState.data
                    //_posts.value = state.data
                    loading.value = false
                }
                is Resource.Failure<*> -> {
                    Log.e(nameState.throwable, nameState.throwable)
                }
            }
        }
    }
    return name
}

The probleme is in HomeScreen I think, when I'm calling the collectasState().value.

HomeScreen

val state = rememberLazyListState()
        LazyColumn(
            state = state,
            verticalArrangement = Arrangement.spacedBy(10.dp)
        ) {
            items(post) { post ->
                //val difference = homeViewModel.getDateTime(homeViewModel.getTimestamp())
                val date = homeViewModel.getDateTime(post.timeStamp!!)
                val name = homeViewModel.getName(post.postAuthor_id.toString()).collectAsState().value
                QuestionCard(
                    name = name.toString(),
                    date = date!!,
                    image = "",
                    text = post.postText!!,
                    like = 0,
                    response = 0,
                    topic = post.topic!!
                )
            }
        }

I can't post video but if you need an image, imagine a textField where the test is alternating between "null" and "MyName" every 0.005 second.

Upvotes: 1

Views: 990

Answers (1)

Emmanuel Montt
Emmanuel Montt

Reputation: 376

Check official documentation.

https://developer.android.com/kotlin/flow

Flow is asynchronous

On viewModel

private val _name = MutableStateFlow<String>("")
    val name: StateFlow<String>
        get() = _name

fun getName(uid: String) {
    viewModelScope.launch {
        //asyn call
        navRepository.getName(uid).collect { nameState ->
            when (nameState) {
                is Resource.Success -> {
                    name.value = nameState.data
                }
                is Resource.Failure<*> -> {
                    //manager error
                    Log.e(nameState.throwable, nameState.throwable)
                }
            }
        }
    }
}

on your view

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    ...
    lifecycleScope.launch {
        viewModel.name.collect { name -> handlename
        }
    }
}

Upvotes: 2

Related Questions