How can I set remember values only once? ``` v...
# compose
c
How can I set remember values only once?
Copy code
val mobileMidValue by rememberSaveable { mutableStateOf(shippingAddress.mobileData.second.orEmpty()) }
    val mobileLastValue by rememberSaveable { mutableStateOf(shippingAddress.mobileData.third.orEmpty()) }
I am using like this, So, I'd like to initialize the value only once?
l
If this doesn't change, why not just
val mobileMidValue = rememberSaveable { shippingAddress.mobileData.second.orEmpty() }
? 🤔
c
it changes when I update address. And the response contains mobile data.
And I don't want to update mobile data from the second response.
mobileMidValue and mobileLastValue are connected to TextField.
So, when it receives new response then, it overwrite. So, input data is removed and response data is in the TextField.
l
you mean, they can be changed through TextField (should be MutableState), but the initial value is from another service?
c
The initial value from API response. And also they are connected to TextField. And when address data changes it will call API and it overwrite all the TextField.
because they observes the response.
I tried with
Copy code
val mobileMidValue by rememberSaveable(Unit) { mutableStateOf(shippingAddress.mobileData.second.orEmpty()) }
or
Copy code
LaunchEffect(Unit) {
    mobileMidValue = shippingAddress.mobileData.second.orEmpty()
    ...
}
Since, the address is empty at first, it just initialize empty String. But, I'd. like to initialize them when there are data. And it will be the first data that I want to initialize.
d
The composable will start with the value in the ViewModel. After that it will ignore any further values output by the ViewModel. Only the value input by the user will be put in the text field for the lifecycle of the Composable. Please refer to this article on text state management: https://medium.com/androiddevelopers/effective-state-management-for-textfield-in-compose-d6e5b070fbe5