Marcin Środa
07/17/2020, 5:01 PMclass MessageViewModel : ViewModel() {
var state: MutableState<TextFieldValue> = state { TextFieldValue() }
}
Cause:
Functions which invoke @Composable functions must be marked with the @Composable annotationZach Klippenstein (he/him) [MOD]
07/17/2020, 5:03 PMLiveData
(which is a more general androidx concept), not compose's State
. That said, if you really want to use the compose tools, use mutableStateOf
instead of state
.Marcin Środa
07/17/2020, 5:59 PMshikasd
07/17/2020, 8:36 PMmutableState
in context like this.
Apart from separating UI - presentation layer, ofcZach Klippenstein (he/him) [MOD]
07/17/2020, 8:39 PMSean McQuillan [G]
07/17/2020, 11:49 PMmutableStateOf
is a great idea. It's a bit more succinct with the same behavior:
val myState: Int by mutableStateOf(0)
private set
If you expect it may be consumed by non-compose UI bits, then LiveData
is a good idea.
We've recently reworked the way state frames work to allow state to be written from a background thread (conflated). This was one of the major motivations previously to use LiveData
in situations like this.Sean McQuillan [G]
07/17/2020, 11:54 PMZach Klippenstein (he/him) [MOD]
07/18/2020, 12:01 AMSean McQuillan [G]
07/18/2020, 12:07 AMby/private set
transformation, which means I can just treat a read outside of compose as "what's the current value presently"
Adding async writes may change that though – and it'd be good to make sure we're providing the API needed for the testing case