Skip to main content
fixed several issues with the provided code
Source Link
tyg
  • 13.5k
  • 4
  • 33
  • 45

you can try using StateFlow something like this: Your ViewModel

private val _repDataPoints = MutableStateFlow<List<RepDataPoint>>(listOfemptyList())
val repDataPoints = _repDataPoints.asStateFlow()

Your Composable:

val list by viewModel.repDataPoints.collectAsState(initial = emptyListcollectAsStateWithLifecycle())

And finally to modify repDataPoints value you can do something like that:

fun addNewValue() {
    viewModelScope.launch {
        val list = _repDataPoints.value.toMutableList()
        list.add(RepoDataPoits())
    update { it + _repDataPoints.emitRepDataPoint(list)
    }
}

I think the problem is your list that you have in you viewmodel cant notify to your composable and never recompose

you can try using StateFlow something like this: Your ViewModel

private val _repDataPoints = MutableStateFlow<List<RepDataPoint>>(listOf())
val repDataPoints = _repDataPoints.asStateFlow()

Your Composable:

val list by viewModel.repDataPoints.collectAsState(initial = emptyList())

And finally to modify repDataPoints value you can do something like that:

fun addNewValue() {
    viewModelScope.launch {
        val list = _repDataPoints.value.toMutableList()
        list.add(RepoDataPoits())
        _repDataPoints.emit(list)
    }
}

I think the problem is your list that you have in you viewmodel cant notify to your composable and never recompose

you can try using StateFlow something like this: Your ViewModel

private val _repDataPoints = MutableStateFlow<List<RepDataPoint>>(emptyList())
val repDataPoints = _repDataPoints.asStateFlow()

Your Composable:

val list by viewModel.repDataPoints.collectAsStateWithLifecycle()

And finally to modify repDataPoints value you can do something like that:

fun addNewValue() {
    _repDataPoints.update { it + RepDataPoint() }
}

I think the problem is your list that you have in you viewmodel cant notify to your composable and never recompose

Source Link
witodev
  • 138
  • 3

you can try using StateFlow something like this: Your ViewModel

private val _repDataPoints = MutableStateFlow<List<RepDataPoint>>(listOf())
val repDataPoints = _repDataPoints.asStateFlow()

Your Composable:

val list by viewModel.repDataPoints.collectAsState(initial = emptyList())

And finally to modify repDataPoints value you can do something like that:

fun addNewValue() {
    viewModelScope.launch {
        val list = _repDataPoints.value.toMutableList()
        list.add(RepoDataPoits())
        _repDataPoints.emit(list)
    }
}

I think the problem is your list that you have in you viewmodel cant notify to your composable and never recompose