Mateu
02/01/2023, 11:35 AMJohn O'Reilly
02/01/2023, 11:41 AMMateu
02/01/2023, 12:13 PMf you would still rather use StateFlow to store state, make sure you’re collecting from the flow using the immediate dispatcher as opposed to the default dispatcher.
This solution requires deeper coroutines knowledge and might lead to issues:
* Since the collection is synchronous, it’s possible the UI is in a not-ready-to-operate-on state when it happens.
* Interferes with Compose’s threading and rendering phases, amongst other things, because it assumes that recomposition happens on the Main thread.
val coroutineScope = rememberCoroutineScope()
val textFlow = remember{ MutableStateFlow("") }
var textFlowState = textFlow.collectAsState(textFlow.value, coroutineScope.coroutineContext)
TextField(
value = textFlowState.value,
onValueChange = { textFlow.value = it }
)
that works correctly on Android, but fails to work on Desktop (the cursor does not move corerctly and letters are reordered).