Skip to content Skip to sidebar Skip to footer

Android Val Cannot Be Reassigned

I defined a variable called notes, when i try to modify it from a method, android studio says val cannot be reassigned however i can modify it if i access the variable like this

Solution 1:

You named two things notes:

privatevar notes = emptyList<Note>()

and:

funsetNotes(notes: List<Note>)

So, in the setNotes() function, notes refers to the function parameter. That is treated as a val and cannot be reassigned. this.notes refers to the notes property defined on your NoteAdapter class.

In general, try to use different names, to reduce confusion. For example, you might use:

funsetNotes(replacementNotes: List<Note>)

Solution 2:

In Kotlin,

val - When you declared any variable in Koltin as final variable so once assigned we cannot changes the value of it.

So use var when you need to define/change value while running application

Post a Comment for "Android Val Cannot Be Reassigned"