I'm learning compose, and one of the screens I'm w...
# compose-desktop
m
I'm learning compose, and one of the screens I'm writing has a number of text fields on it. to make things easier on myself, I put the text fields into a mutable list, and I'm finding that i can't update the text field input if I try to update a text saver through the list. When I added in logging, the
it
only ever registered the first key pressed, as if the previous keystrokes were not saved. Did I miss something in how this works? example of what I'm trying to do (if needed, I can provide more than just the barebones) :
Copy code
var textSaver1 by rememberSaveable(stateSaver = TextFieldValue.Saver) { mutableStateOf(TextFieldValue()) }
. . .create 15 more

var textSavers1Through4 = mutableListOf(textsaver1, textsaver2, textsaver3, textsaver4)

Box {
 OutlinedTextField( value = textSavers1Through4[0],
                    onValueChange = {
                        textSavers1Through4[0] = it 

                    }
                )
            }
In case it matters, I'm using Kotlin version 1.8.20 and compose version 1.4.0. (And just to be clear - it works as expected if I don't use the mutable list, I'm just confused as to why the mutable list seems to make it not work)
s
not too familiar with what you're trying to do here but could you mean
Copy code
textSavers1Through4[0].value = it?
m
maybe? i'll give it a try
s
i mean, logically you're putting these "text savers" into a mutable list, swapping one out for something else doesn't modify it, in fact you probably don't want a mutable list, if i understand what you're wanting you just want to put them in a list for convenience?
m
pretty much - i mostly put them in a mutable list because i couldn't use
val
for the textsavers originally
And yeah - it throws a compilation error if I try to use listOf
s
what's the error?
m
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public inline operator fun kotlin.text.StringBuilder /* = java.lang.StringBuilder */.set(index: Int, value: Char): Unit defined in kotlin.text
And it's referencing this :
textSavers1Through4[0] = it
s
i don't think compose can work this way. might want to rethink the architecture here a little
m
potentially - and that's what i wanted to check.
s
not sure rememberSaveable is the thing here, but i'm not exactly sure why you went for it (first time seeing it for me but the doc doesn't sound like it's what you want)
m
It was just what was in the tutorial for OutlinedTextField.
even swapping it to a more basic by remember { _mutableStateOf_("") } didn't change the observed behavior. Shrugs i'll just go back to updating the object directly. Thanks again for your help.
s
I think what you want is viemodels to persist things between process recreation, there's probably a way to do it with rememberSaveable though
m
good to know!