Pages

Tuesday, September 13, 2022

Update particular item's value from a list of data class Kotlin

 Your data class should have mutable properties so that they can be changed:

fun <T> updateWorkerData(id: Int, property: KMutableProperty1<Worker, T>, value: T) {
    val workers = getListOfWorker()
    workers.forEach {
        if (it.id == id) {
            property.set(it, value)
        }
    }
}

updateWorkerData(1, Worker::age, 28)

original post

No comments:

Post a Comment