androidkotlinandroid-jetpack-compose

Type 'TypeVariable(T)' has no method 'getValue(Nothing?, KProperty<*>)' and thus it cannot serve as a delegate


I'm practicing, trying to learn compose on my own. I want to create a list that I can add elements in runtime and they will be displayed on the screen, so I need to use the remember delegate and a StateList.

I've added manually the imports from androidx.compose.runtime since Android Studio wouldn't pick them automatically. I also tried to clean, rebuild, restart, invalidate cache... all the tips I've found online when looking up this error message. But it always comes back to the same thing: the remember block (between ** ** below) is highlighted in red and the error message is the one on the title. I'm running out of ideas on how to make this work

import androidx.compose.runtime.*
import androidx.compose.runtime.snapshots.SnapshotStateList   
 ...
...
@Composable
    fun MainContent() {
        ListingTheme {
    
            val elements: SnapshotStateList<String> by remember { **mutableStateListOf()** }
            val buttonAciton: (String) -> Unit = { enteredText ->
                elements.add(enteredText)
                Log.d("MainContent", "Button Clicked, current list: $elements")
            }

Solution

  • It doesn’t make sense to use the list as a delegate for a list. It already is a list. Replace by with =.