Aman
Aman

Reputation: 68

Unable to update the value of Basic Text Field

I was building an app the uses Basic Text Field. When I try to update the value of Basic Text Field, it won't work unless you recompose it.

val textX = remember{ mutableStateOf("Text")}


BasicTextField(
            value = textX.value,
            onValueChange = {
                textX.value = it
                            }  )

Text(text = textX.value)

When the textX change in other place of code, it won't update on the Basic Text Field, but it updates in Text composable Live. But when i try to recompose by navigating to other screen and come back, it will display correctly.

But it's obvious that it can be updated on the UI.

I have tried using "by" but not working.

Upvotes: 2

Views: 78

Answers (1)

Robert Jamison
Robert Jamison

Reputation: 299

You are using a val which can only be set to a value once, instead of a var which allows the value to be updated multiple times.

OPTIONAL: instead of this:

val textX = remember{ mutableStateOf("Text")}

Use the by setter:

val textX by remember{ mutableStateOf("Text")}

Upvotes: 0

Related Questions