0
//declared in activity
val statebilledproducts = viewModel.listBilledProducts

//declared in viewmodel class OrderViewModel : ViewModel() {

private val _listBilledProducts  = mutableStateListOf<BilledProducts>()
val listBilledProducts :List<BilledProducts> = _listBilledProducts

fun Quantityincrement(index:Int){ viewModelScope.launch {

        _listBilledProducts[index].quantity=_listBilledProducts.value[index].quantity+1
        _listBilledProducts[index].productprice = _listBilledProducts.value[index].unitprice * _listBilledProducts.value[index].quantity
      
    }

}
fun QuantityDecrement(index:Int){
    viewModelScope.launch {
        _listBilledProducts[index].quantity+=1
        _listBilledProducts[index].productprice = _listBilledProducts.value[index].unitprice * _listBilledProducts.value[index].quantity
    }

}
fun productRemove(index:Int){
    viewModelScope.launch {
        _listBilledProducts.removeAt(index)
    }

}

}

i tried but state of jet pack Compose is not observing

1 Answers1

3

mutableStateListOf makes the list, not its content, observable. In other words, Compose will be notified if the list changes such as when items are changed, inserted, or deleted, not when the instances, such as the BilledProducts, are changed.

Consider using an data class for BilledProducts instead of a mutable class. For example,

data class BilledProduct(
    val quantity: Int,
    val unitPrice: Double
) {
  val productPrice get() = quantity * unitPrice
}

then incrementing the quantity would be,

fun QuantityIncrement(index:Int) {
    viewModelScope.launch {
        val lastBilled = _lastBilledProduces[index]
        _listBilledProducts[index] = lastBilled.copy(quantity = lastBilled.quantity + 1)
    }
}
chuckj
  • 27,773
  • 7
  • 53
  • 49