Is smart cast for nullable State fields something ...
# compose
y
Is smart cast for nullable State fields something that could be improved in Compose? Seems like a case for https://youtrack.jetbrains.com/issue/KT-20294, The composition should run within a snapshot of the state system, where a field is null or not. Seems like the taskSeries should be known to be non-null.
Copy code
val taskSeries by viewModel.taskSeries(taskSeriesId).collectAsState(initial = null)

    if (taskSeries != null) {
                Text(text = taskSeries.name)
d
Hi @yschimke, how can
taskSeries
be not null during the first composition if you explicitely specify
null
as an initial value for your
collectAsState
?
y
The
if (taskSeries != null) {
check handles that, but this
Text(text = taskSeries.name)
shows the error
Smart cast to 'TaskSeries' is impossible, because 'taskSeries' is a property that has open or custom getter
I guess I'm asking whether Compose should do something similar to Kotlin contracts
d
Oh I am sorry. Reading your post, I though you wanted to get rid of this check. Please ignore my previous comment. I can't explain the error without more context.
s
In this case, since you're dealing with a
val
on the Compose side, you could get rid of the property delegate and just do this:
Copy code
val taskSeries = viewModel.taskSeries(taskSeriesId).collectAsState(initial = null).value
That should help your smart casting along a little.