kotlin Flow is not updating realtime

93 Views Asked by At

I have a flow within an interface, and I'm trying to collect values in the ViewModel, but the collect operation isn't being triggered. Can anyone help me figure out what I might be doing wrong?

I am injecting using dagger hilt

@Module
@InstallIn(SingletonComponent::class)
object RealtimeListenerModule {

    @Provides
    fun bindingFunction() : RealtimeListener{
        return RealtimeListenerImpl()
    }

}
interface RealtimeListener {
    val user: Flow<User?>
}
class RealtimeListenerImpl : RealtimeListener {
    
    private val _userData = MutableStateFlow<User?>(null)
    override val user: Flow<User?> = _userData
    
    //this function will trigger when any events comes from firebase and i tested events are firing fine        
    fun updateUserData(){
    _userData.value = it
    }       


}
@HiltViewModel
class UserViewModelImpl
@Inject constructor(private val listener: RealtimeListener) : UserViewModel() {
    init {
        viewModelScope.launch {
            listener.user.collect {
                Log.d("TAG", "trigger $it")
            }
        }
    }
}

In UserViewModelImpl Log.d("TAG", "trigger $it") is executing only one Time when init{} block called now after some time any new event trigger i am not able to receive value in collect block ,

also i know that init block will trigger only once , i want is that when flow emit value every time i want to update user i need help to achive that thanks in advance :-)

0

There are 0 best solutions below