1

In my app, the user can submit a form for a Person, and later on they can edit that form. This brings up a screen with multiple TextFields that are populated with the Person's info. I'm having an issue where when I edit one form and then go to edit another, the TextFields contain the info from the first Person instead of the current Person. This is how I'm implementing it:

@Composable
fun FormContent(
    person: Person? // nullable because I'm retrieving from database and there could be an exception
) {
    var name by rememberSaveable(person) { mutableStateOf(person?.name.orEmpty()) }
    var age by rememberSaveable(person) { mutableStateOf(person?.age.orEmpty()) }
    var gender by rememberSaveable(person) { mutableStateOf(npc?.gender.orEmpty()) }

    // These are inside a Column
    TextField(
        label = stringResource(id = R.string.name),
        onValueChangeAction = { name = it },
        startingText = person?.name.orEmpty()
    )

    TextField(
        label = stringResource(id = R.string.age),
        onValueChangeAction = { age = it },
        startingText = person?.age.orEmpty()
    )

    TextField(
        label = stringResource(id = R.string.gender),
        onValueChangeAction = { gender = it },
        startingText = person?.gender.orEmpty()
    )
}

I've added the current person as an input for rememberSaveable so that it gets reset when a new Person is passed in. However, when the first composition occurs, fields are set to the previous Person's info, but then the fields are set to the new Person's info upon subsequent recompositions, yet the TextFields aren't updating. I've debugged and added logging and can see the subsequent recompositions do have the updated Person, but the startingText for the TextFields stays the same. This is what logcat prints:

Person(id=15, name=Bob, age=16, gender=Male) // Previous person

Person(id=16, name=Amy, age=20, gender=Female) // Current person

Person(id=16, name=Amy, age=20, gender=Female)

From my understanding the TextFields would get recomposed when person changes, but that doesn't seem to be the case. Can someone let me know what I'm doing wrong?

pjp94
  • 13
  • 3
  • After the click on the second person to edit their info the previous person shows but then a recomposition happens and the new one(right one) is shown? If that is so what causes that recomposition is it you or it recomposes automatically because the new person is loaded – Ayman Ait Aug 06 '23 at 23:22
  • I retrieve the person from the database and then emit it to the StateFlow, which is being observed in another composable. When the StateFlow changes with a new person, I call this FormContent composable to show the form. – pjp94 Aug 06 '23 at 23:51
  • I don't know your project structure but what i would basically do is let's when i click on a person that open a new screen with the form to edit them so what i would do is on click of this person i would start a launch effect block that call a suspend function from viewmodel what this function does is retreive the right person from DB and then put it in the state flow var after this suspend function is finished only then i navigate to the screen with the form – Ayman Ait Aug 07 '23 at 15:18

0 Answers0