Thread
#compose
    d

    dimsuz

    2 years ago
    I must miss something obvious, but why
    var inputState: TextFieldValue by mutableStateOf(TextFieldValue())
    fails with an error
    Type 'MutableState<TypeVariable(T)>' has no method 'getValue(Nothing?, KProperty<*>)' and thus it cannot serve as a delegate
    flosch

    flosch

    2 years ago
    Either
    var inputState: TextFieldValue = mutableStateOf(TextFieldValue())
    or
    var inputState: TextFieldValue by remember { mutableStateOf(TextFieldValue()) }
    d

    dimsuz

    2 years ago
    both give the similar error 😞
    d

    Daniel Sandström

    2 years ago
    You need to import
    androidx.compose.runtime.getValue
    androidx.compose.runtime.setValue
    d

    dimsuz

    2 years ago
    great this worked, thanks! What is the difference between using delegate and "remember"?
    flosch

    flosch

    2 years ago
    delegation and
    remember
    cannot be compared.
    remember
    can be called by delegation (getValue/setValue),
    mutableStateOf
    cannot.
    Sorry scratch that,
    mutableStateOf
    , can be called by delegation
    d

    dimsuz

    2 years ago
    hmm. I can see this in the codelab:
    var todoItems: List<TodoItem> by mutableStateOf(listOf())
    no remember and delegation. But it doesn't work when I try to feed TextFieldValue like this
    Only using
    by remember { mutableStateOf(...) }
    works
    flosch

    flosch

    2 years ago
    You should read through the code-lab to find out what
    remember { X }
    ,
    mutableStateOf(X)
    and
    remember { mutableStateOf(X) }
    are doing.
    d

    dimsuz

    2 years ago
    They cause recomposition If value changes I guess. But I'm not sure I understand what's the point of
    by mutableStateOf
    then
    will read this codelab in full then 🙂
    flosch

    flosch

    2 years ago
    You can read about delegated properties here. This is a kotlin thing, not a compose thing.
    d

    dimsuz

    2 years ago
    If
    by mutableStateOf
    simply adjusts property to delegate to state's get/set, then I don't understand why codelab's examples work without remember:
    var todoItems: List<TodoItem> by mutableStateOf(listOf())
    propagates changes to composables. While if I do this with
    TextFieldValue
    it isn't propagating. only if I additionaly wrap in
    remember
    — then it does.