Hi all, I'm having some problems when using a sta...
# compose-desktop
m
Hi all, I'm having some problems when using a state flow to store the value of a TextField. I have the following sample code:
Copy code
val coroutineScope = rememberCoroutineScope()
val textFlow = remember{ MutableStateFlow("") }
var textFlowState  = textFlow.collectAsState(textFlow.value, coroutineScope.coroutineContext)
TextField(
    value = textFlowState.value,
    onValueChange = { textFlow.value = it }
)
In Android works great, but in compose Desktop when you enter the text fast the cursor does not behave correctly, and some chars are not added to the correct position. I understand that the problem must be related to this post: https://medium.com/androiddevelopers/effective-state-management-for-textfield-in-compose-d6e5b070fbe5 How can I make it work in both places?
a
That does seem to explain the problem you’re seeing. Are you asking why it works in Android?
m
For what I understand, as I'm using collectAsState with the current correoutineContext, the collect as state is called in the same Dispatcher as Compose so it should work. What I'm looking is for a way that works in all the platforms.
a
Even if it’s called in the same thread, it can be called “later”.
m
Then, how can I use a StateFlow with a TextField in desktop?
a
I’m not sure you can. Why do you need to? Maybe there’s a work-around for your situation.
l
I ran into an issue last week with Compose for Desktop, where the cursor is always in front of the last character instead of behind (if typing slowly, the word would show in reverse of what you type). I wonder if this is the same thing you’re seeing.
m
I'm working on KMP with shared viewmodels, therefore viewmodels need to work with no compose classes. Each viewModel produces a flow with the viewmodel that compose "renders".
@Landry Norris The problem appears when I write fast. Its a race problem
b
Having the same problem. Did any of you found a solution?
It seems that
_.collectAsState_(Dispatchers.Main.immediate)
helps.
m
I forgot that I already asked this here. I had no solution and I created a new thread: https://kotlinlang.slack.com/archives/C01D6HTPATV/p1683299929192579