https://kotlinlang.org logo
#compose-desktop
Title
# compose-desktop
m

Mateu

02/07/2023, 9:55 AM
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

Alexander Maryanovsky

02/07/2023, 10:38 AM
That does seem to explain the problem you’re seeing. Are you asking why it works in Android?
m

Mateu

02/07/2023, 11:00 AM
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

Alexander Maryanovsky

02/07/2023, 11:02 AM
Even if it’s called in the same thread, it can be called “later”.
m

Mateu

02/07/2023, 11:16 AM
Then, how can I use a StateFlow with a TextField in desktop?
a

Alexander Maryanovsky

02/07/2023, 11:17 AM
I’m not sure you can. Why do you need to? Maybe there’s a work-around for your situation.
l

Landry Norris

02/07/2023, 6:32 PM
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

Mateu

02/17/2023, 2:33 PM
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

benkuly

04/21/2023, 3:35 PM
Having the same problem. Did any of you found a solution?
It seems that
_.collectAsState_(Dispatchers.Main.immediate)
helps.
m

Mateu

05/10/2023, 10:10 AM
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
23 Views