LazyColumn not update when data in LiveData update

929 Views Asked by At

I want to create chat app UI using ViewModel but when I send button the List in ViewModel update but LayzColumn not update it data. I don't know why it's not working.

My ChatViewModel

class ChatViewModel: ViewModel() {
    private val _messages: MutableLiveData<MutableList<String>> = MutableLiveData(mutableListOf(""))

    val messages: LiveData<MutableList<String>> get() = _messages

    fun add(message: String) {
        _messages.value?.add(message)
        Log.d("Haha", "${_messages.value?.size}")
        _messages.notifyObserver()
    }

}

fun <T> MutableLiveData<T>.notifyObserver() {
    this.value = value
}

My LazyColumn

val messages by chatViewModel.messages.observeAsState(mutableListOf(""))


LazyColumn(modifier = Modifier
            .fillMaxWidth()
            .weight(1f)
        ) {
            items(messages.toList()) { message ->
                SenderChat(message = message)
            }

        }
0

There are 0 best solutions below