I'm running into a strange issue. The following code should update the text field value, and close t...
z
I'm running into a strange issue. The following code should update the text field value, and close the keyboard. However, the onValueChange from the TextField is resetting it back to the prior value right after the selectSuggestion changes it. If I add a 20 ms delay between the two calls then it behaves correctly. What's going on?
Copy code
.clickable {
    coroutineScope.launch {
        viewModel.selectSuggestion(suggestion)
        focusManager.clearFocus()
    }
}
Copy code
TextField(
    value = viewModel.textFieldValue,
    onValueChange = viewModel::textFieldValueChange
)
Copy code
var textFieldValue by mutableStateOf(TextFieldValue())
    private set
Copy code
fun selectSuggestion(suggestion: String) {
    textFieldValue = TextFieldValue(
        text = suggestion,
        selection = TextRange(suggestion.length)
    )
}
Copy code
fun textFieldValueChange(value: TextFieldValue) {
    textFieldValue = value
}
z
The 20ms delay probably works because it waits for a frame.
withFrameNanos
would probably work too. That means there’s probably a race condition in the field where a focus change causes the old internal state to be used instead of the newly composed value. Please file a bug - might not be fixed in the current field but there are a bunch of these kinds of bugs in the current impl that we’re hoping to fix as we rewrite the TextField impl.
z
Will do soon. Thank you