Why does this compile? `var text: String by mutabl...
# compose
r
Why does this compile?
var text: String by mutableStateOf(text)
(At runtime it gets "Attempt to invoke interface method 'java.lang.Object androidx.compose.runtime.State.getValue()' on a null object reference.)
z
it looks like perfectly valid code. The runtime issue seems like something’s wrong, maybe a version mismatch?
r
What does the line mean? It seems circular.
var text: MutableState<String> = mutableStateOf(text)
gives "Type mismatch. Required: String Found: MutableState<String>"
a
yeah I don't think this is compose-specific. It's letting you refer to the property of the class that you're declaring in its own initializer expression
and its delegate is predictably null while it's running that initializer expression to create that delegate
out of context it's not unusual to have that exact line of code...when there's also a primary constructor parameter with the same name
r
That's how I ran across it -- I deleted the constructor parameter without changing anything else. Thanks.
👍 1