@Composable
fun EditTodoList(item: ToDoData, onEditComplete: (String, Int) -> Unit) {
var editedTitle by remember { mutableStateOf(item.title) }
var editedQuantity by remember { mutableStateOf(item.quantity.toString()) }
Row(
modifier = Modifier.fillMaxSize(),
horizontalArrangement = Arrangement.SpaceBetween
) {
Column {
TextField(
value = editedTitle,
onValueChange = { editedTitle = it },
label = { Text("Title") }
)
TextField(
value = editedQuantity,
onValueChange = { editedQuantity = it },
label = { Text("Quantity") },
)
}
Button(onClick = {
}) {
Text("Save")
}
}
}
**On -> var editedTitle by remember { mutableStateOf(item.title) }
and -> var editedQuantity by remember { mutableStateOf(item.quantity.toString()) }
When i am using item.title and item.quantity inside this it's showing me value captured in closure and item is of type ToDoData which is of type Data Class.
How to solve this error?
I just want to store the value for editTitle and editedQuantity
@Composable
fun EditTodoList(item: ToDoData, onEditComplete: (String, Int) -> Unit) {
var editedTitle by remember { mutableStateOf(item.title) }
var editedQuantity by remember { mutableStateOf(item.quantity.toString()) }
Row(
modifier = Modifier.fillMaxSize(),
horizontalArrangement = Arrangement.SpaceBetween
) {
Column {
TextField(
value = editedTitle,
onValueChange = { editedTitle = it },
label = { Text("Title") }
)
TextField(
value = editedQuantity,
onValueChange = { editedQuantity = it },
label = { Text("Quantity") },
)
}
Button(onClick = {
}) {
Text("Save")
}
}
}
**On -> var editedTitle by remember { mutableStateOf(item.title) }
and -> var editedQuantity by remember { mutableStateOf(item.quantity.toString()) }
When i am using item.title and item.quantity inside this it's showing me value captured in closure and item is of type ToDoData which is of type Data Class.
How to solve this error?
I just want to store the value for editTitle and editedQuantity
Share Improve this question edited Mar 25 at 7:31 tomerpacific 6,60818 gold badges41 silver badges60 bronze badges asked Mar 25 at 6:47 Global HelperGlobal Helper 31 bronze badge 2- Please edit the question to provide the full error message and point to the line in your code where it appears. As it is now it all looks fine, I don't see any problems. – tyg Commented Mar 25 at 8:29
- Please edit the title of your question to be descriptive, unambiguous, and specific to what you are asking. For more guidance, see How do I write a good title?. – tyg Commented Mar 25 at 8:35
1 Answer
Reset to default 0It's not a good idea to store the value that comes from parameters into an state in a rememeber
as it cause your composable not to accept new values so if you change the ToDoData's value from the parent composable, compose tries to recompose your UI but as you stored the title into an state and it's remember
ed you don't see new values on the UI.
another pitfall of this approach is that if you rotate your phone (or any other configuration changes) your state will be restarted to the initial value.
to fix that try to put your state in your view model and just ask your view model to change the values from your composable:
@Composable
fun EditTodoListScreen(
viewModel : EditTodoListViewModel = hiltViewModel()
) {
val item by viewModel.item.collectAsState()
EditTodoList(
item,
{ viewModel.changeTitleTo(it) },
{ viewModel.changeQuantityTo(it) },
{}
)
}
@Composable
fun EditTodoList(
item: ToDoData,
onTitleChanged: (String) -> Unit,
onQuantityChanged: (String) -> Unit,
onEditComplete: (String, Int) -> Unit) {
Row(
modifier = Modifier.fillMaxSize(),
horizontalArrangement = Arrangement.SpaceBetween
) {
Column {
TextField(
value = item.title,
onValueChange = { onTitleChanged(it) },
label = { Text("Title") }
)
TextField(
value = item.quantity,
onValueChange = { onQuantityChanged(it) },
label = { Text("Quantity") },
)
}
Button(onClick = {
}) {
Text("Save")
}
}
}
and in your ViewModel:
@HiltViewModel
class EditTodoListViewModel @Inject constructor() {
val item = MutableStateFlow(ToDoData())
fun changeTitleTo(newTitle : String) {
item.update {
it.copy(title = newTitle)
}
}
// ....
}
In this case you have the latest changes in your view model and you always recover from configuration changes.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744214144a4563471.html
评论列表(0条)