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

前端 未结 4 1177
我寻月下人不归
我寻月下人不归 2021-02-13 02:53

I\'m trying to get a value from LiveData with observeAsState in jetpack compose, but I get a weird error

Type \'State\' has no method \'getValue

4条回答
  •  时光说笑
    2021-02-13 03:16

    To fix the error add the following imports:

    // for a 'val' variable
    import androidx.compose.runtime.getValue
    
    // for a `var` variable also add
    import androidx.compose.runtime.setValue
    
    // or just
    import androidx.compose.runtime.*
    

    To use a variable as a property delegate you should provide getValue operator function for read-only val variables and getValue and setValue functions for var variables.

    To read more about how property delegates and state are combined in jetpack compose see Use remember to create internal state in a composable documentation section. There's also an explanation in Thinking in Compose video.

提交回复
热议问题