1

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.

0

1 Answer 1

2

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
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer, I'll test that :)-

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.