Hello all, I'm stumped trying to get a `Text` obje...
# compose-desktop
k
Hello all, I'm stumped trying to get a
Text
object's contents in a
@Composable
to update when a global String variable changes. I have a global String
var dataIn = ""
that regularly fills with characters like
data += char
in one function, but I can't get its value change to recompose the displayed
Text
in the other function! I thought it should be doable using
var dataDisplay: String = remember { mutableStateOf(dataIn) }
and
Text(text = dataDisplay)
but it won't update the text display every time a new char is added to the
dataIn
variable. Thanks again!
m
Why should it change? By definition it is only read the first time when
dataDisplay
is initialised. That’s what
remember
is made for.
k
This is what I need to get my head around. I might need to use another command?
m
You’ll have to explicitly update dataDisplay if you want to change it.
It might be better to introduce some StateFlow in your model which you can then observe in your Composable.
k
Hmm. So once dataIn has been updated by appending a Char, I need to then update the dataDisplay variable somehow?
OK! So I need to learn about StateFlow then?
Observing external values sounds like what I'm after! If I can control the UI updates within the screen function that holds the Text object, that would be good?
z
Why can't you just make your global var a MutableState?
Better yet, avoid using a global in the first place
Ah, it looks like you figured out the mutable state thing in another thread.
k
Hey @Zach Klippenstein (he/him) [MOD]! Thanks very much for your onto and attention mate. Yes I did eventually twig, and found that I could indeed use that method! 😆 What more can you tell me about avoiding global vars now? Thanks!