Hi all I am getting color from state different in ...
# compose
l
Hi all I am getting color from state different in two places in same screen. State class:
Copy code
data class AddEditNoteState(
   val color: Int = Note.noteColors.random().toArgb(),
)
In ViewModel:
Copy code
private val _addEditNoteSate = MutableStateFlow(AddEditNoteState())
val addEditNoteState = _addEditNoteSate
   .asStateFlow()
   .stateIn(viewModelScope, WhileViewSubscribed, AddEditNoteState())
In Compose:
Copy code
val addEditNoteState by rememberFlowWithLifecycle(viewModel.addEditNoteState).collectAsState(AddEditNoteState())
val noteBackgroundColor = remember {
   Animatable(
      Color(if (noteColor != -1) noteColor else addEditNoteState.color)
   )
}
Now when I access addEditNoteState.color and noteBackgroundColor.value Both are different. Not able to find the error. Note (noteColor != -1) is not true means we are using addEditNoteState.color in Animatable*
z
So you’re initializing the
noteBackgroundColor
to
addEditNoteState.color
, and then they’re different – does `addEditNoteState`’s value change after that initialization?
l
There was a problem in using rememberFlowWithLifecycle with collectAsState() in compose. Only using collectAsState() will work perfectly.