So, I have some code that basically boils down to ...
# compose
s
So, I have some code that basically boils down to this:
Copy code
class State {
	var item by mutableStateOf<IClass?>(null)
}

@Composable
fun SomeUi(state: State) {
	Text("Item is ${state.item}")
}
SomeUi()
is rendered to screen, and
state.item
is changed from
null
to some value a little afterwards. But
SomeUi
never picks up on that update and thus the text keeps saying
"Item is null"
. But the way I understand the Compose state system, this should work fine, shouldn't it? I can verify that
state.item
does change its value (by just periodically printing it in a coroutine). What's odd is that if I experimentally change
IClass?
to
String?
it works again. I want to put more effort into debugging it, but first I'd like to verify that I'm not simply misunderstanding something about State and how it's supposed to work.
r
Where are you calling
SomeUI
from?
s
Nothing special. Some other composable, and eventually a top-level composable. Boiled down, it's basically like this:
Copy code
application {
	val state = remember { State() } 
	LaunchedEffect(Unit) { // just an example
		delay(100)
		state.item = getMeSomeItem()
	}
	SomeUi(state)
}
The real code is much more involved, but conceptually it's this. Edit: added the
remember { }
r
You should remember your state.
val state = remember{ State() }
s
Sorry, my bad! Yes, it is remembered.
r
I just tried this and it worked
❤️ 1
s
Yeah, I couldn't reproduce it either. Simple examples work, but I can see that the problem exists in my codebase. That's why I can't give a reproducer for the problem. Hence my question of whether this should work or not. I thought, maybe I overlooked a requirement like a stability guarantee for
state
or
item
, or something else. For example if someone had told me "Yeah your State needs to be @Stable for this to work" I could have stopped debugging the problem. But if the answer is "No, this should always work" then I have to do more in-depth debugging to pin point the reason why it doesn't work in this case.
But thank you for taking the time to write the reproducer! I appreciate it.
z
Yep that definitely should work, as written