(#) StateFlow.value should not be called within composition
!!! ERROR: StateFlow.value should not be called within composition
This is an error.
Id
: `StateFlowValueCalledInComposition`
Summary
: StateFlow.value should not be called within composition
Severity
: Error
Category
: Correctness
Platform
: Any
Vendor
: Jetpack Compose
Identifier
: androidx.compose.runtime
Feedback
: https://issuetracker.google.com/issues/new?component=612128
Min
: Lint 8.0 and 8.1
Compiled
: Lint 8.0 and 8.1
Artifact
: [androidx.compose.runtime:runtime-android](androidx_compose_runtime_runtime-android.md.html)
Since
: 1.5.0
Affects
: Kotlin and Java files and test sources
Editing
: This check runs on the fly in the IDE editor
Implementation
: [Source Code](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:/compose/runtime/runtime-lint/src/main/java/androidx/compose/runtime/lint/ComposableStateFlowValueDetector.kt)
Tests
: [Source Code](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:/compose/runtime/runtime-lint/src/test/java/androidx/compose/runtime/lint/ComposableStateFlowValueDetectorTest.kt)
Copyright Year
: 2021
Calling StateFlow.value within composition will not observe changes to
the StateFlow, so changes might not be reflected within the composition.
Instead you should use stateFlow.collectAsState() to observe changes to
the StateFlow, and recompose when it changes.
(##) Example
Here is an example of lint warnings produced by this check:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text
src/androidx/compose/runtime/foo/TestFlow.kt:20:Error: StateFlow.value
should not be called within composition
[StateFlowValueCalledInComposition]
testFlow.value
-----
src/androidx/compose/runtime/foo/TestFlow.kt:24:Error: StateFlow.value
should not be called within composition
[StateFlowValueCalledInComposition]
stateFlow.value
-----
src/androidx/compose/runtime/foo/TestFlow.kt:25:Error: StateFlow.value
should not be called within composition
[StateFlowValueCalledInComposition]
testFlow.value
-----
src/androidx/compose/runtime/foo/TestFlow.kt:29:Error: StateFlow.value
should not be called within composition
[StateFlowValueCalledInComposition]
stateFlow.value
-----
src/androidx/compose/runtime/foo/TestFlow.kt:30:Error: StateFlow.value
should not be called within composition
[StateFlowValueCalledInComposition]
testFlow.value
-----
src/androidx/compose/runtime/foo/TestFlow.kt:39:Error: StateFlow.value
should not be called within composition
[StateFlowValueCalledInComposition]
stateFlow.value
-----
src/androidx/compose/runtime/foo/TestFlow.kt:40:Error: StateFlow.value
should not be called within composition
[StateFlowValueCalledInComposition]
testFlow.value
-----
src/androidx/compose/runtime/foo/TestFlow.kt:43:Error: StateFlow.value
should not be called within composition
[StateFlowValueCalledInComposition]
stateFlow.value
-----
src/androidx/compose/runtime/foo/TestFlow.kt:44:Error: StateFlow.value
should not be called within composition
[StateFlowValueCalledInComposition]
testFlow.value
-----
src/androidx/compose/runtime/foo/TestFlow.kt:50:Error: StateFlow.value
should not be called within composition
[StateFlowValueCalledInComposition]
stateFlow.value
-----
src/androidx/compose/runtime/foo/TestFlow.kt:51:Error: StateFlow.value
should not be called within composition
[StateFlowValueCalledInComposition]
testFlow.value
-----
src/androidx/compose/runtime/foo/TestFlow.kt:55:Error: StateFlow.value
should not be called within composition
[StateFlowValueCalledInComposition]
stateFlow.value
-----
src/androidx/compose/runtime/foo/TestFlow.kt:56:Error: StateFlow.value
should not be called within composition
[StateFlowValueCalledInComposition]
testFlow.value
-----
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Here is the source file referenced above:
`src/androidx/compose/runtime/foo/TestFlow.kt`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~kotlin linenumbers
package androidx.compose.runtime.foo
import androidx.compose.runtime.Composable
import kotlinx.coroutines.flow.*
val stateFlow: StateFlow