How to handle 'val cannot be reassigned' in kotlin?

Example:

val immutableVar = "initial"
immutableVar = "new value"

In Kotlin, 'val' denotes an immutable (read-only) variable. This error occurs when you attempt to reassign a value to it.

Solution:

If you need a mutable variable, use 'var' instead of 'val'.


var mutableVar = "initial"
mutableVar = "new value"

Beginner's Guide to Kotlin