I have a textfield with a problem. If i write the ...
# compose
p
I have a textfield with a problem. If i write the first letter, for example, "a", is ok, if I write something more, for example, "aa", then I can see "aa" for some miliseconds on the textfield and instantly is reverted back to "a" again.
Copy code
OutlinedTextField(value = uiState.searchText, onValueChange = onQueryChange)
onQueryChange is calling this function on the viewmodel which is updating the uiState:
Copy code
fun updateSearchText(searchText: String) {
    viewModelScope.launch {
        userPreferencesRepository.saveSearchTextPreference(searchText)
        updateSearchResults()
    }
}
This is the part that updates the uiState:
Copy code
private suspend fun updateSearchResults() {
    val searchText = userPreferencesRepository.searchText.first()
    val searchResult = if (searchText != "")
        flightRepository.getAirportsByIatOrName(searchText).filterNotNull().first()
    else
        emptyList()

    val favorites = flightRepository.getFavorites().filterNotNull().first()

    uiState = uiState.copy(
        searchResult = searchResult,
        favorites = favorites,
        searchText = searchText
    )
}
This is the datastore function which stores the value of searchText:
Copy code
suspend fun saveSearchTextPreference(searchText: String) {
    dataStore.edit { preferences ->
        preferences[SEARCH_TEXT] = searchText
    }
}
e
p
what do you mean?
when working with Room i'm forced to use a coroutine and also when working with datastore, so as codelabs told I must do that from a coroutine
e
read the article
p
well, I'm a newbie, and after reading the article I don't know what should I change to make this work
even I still don't understand where is the problem
I'm very frustrated with Flow and maybe the issue is there, I still don't understand everything from Flow
maybe you can help me telling me where is the problem exactly
I'm doing what the article says, I'm storing in the datastore variable the new value returned by the field
and I'm reading the datastore variable on the field
e
the solution at the bottom of the article is what you should follow, at least until the replacement BasicTextField2 is stable and merged up into a new TextField
you are not synchronously updating the text
p
well, how can simply using a textfield be that extremely complex in compose?
I suposse you refeer to the part they say that I must do it synchronously, but I don't know how to do it, because I'm forced to use coroutines
e
you can still update other parts of the state in a coroutine. but you must update the text immediately
text is hard, there are multiple asynchronous processes involved. the original compose API is known to be difficult to use, that's why they're working on a new one. it's not done yet.
p
well, I'm trying to figure out how to apply that syncrhonous logic to my source
and I can't see how
they do this: username.value = input
i don't have that kind of variable, the value of the text is inside the uistate which holds data from the database etc... and is stored on a datastore, and datastore forces you to use a coroutine too
e
I have no idea what your codelab is telling you to do but that just can't work without another state holder
p
they are asking to store that search text into a datastore
and following the single source of true of compose, I'm forced to store it on the datastore and reading it from the datastore into the uistate after that
that means I need to use a coroutine
e
for text fields (and BTF2 will force this) the single source of truth is the text state itself, it cannot be the datastore
p
wtf
well, thank you very much, I did it using your solution and worked like a charm, I removed the search text from the uistate and stored it in a by mutablestateof variable and extracted it outside the coroutine and now it works perfectly
I hope they will improve these things, it was much more easy to work without compose, even with java
e
https://developer.android.com/jetpack/androidx/compose-roadmap text editing improvements are being worked on
p
yep, i readed that also on the post you showed me
thank you
on the other hand, maybe can you give me your opinion here too please?