El tipo de argumento ‘Function’ no puede ser asignado al tipo de parámetro ‘void Function(bool?)’.
Mientras estaba creando una aplicación, ocurrió un error que dice: El tipo de argumento ‘Function’ no se puede asignar al tipo de parámetro ‘void Function(bool?)?’. dentro del widget sin estado Taskcheckbox en onChanged: toggleCheckState
dice que la función toggleCheckboxState
no se puede asignar.
“`
class task_tile extends StatefulWidget {
@override
State<task_tile> createState() => _task_tileState();
}
class _task_tileState extends State<task_tile> {
bool ischanged = false;
@override
Widget build(BuildContext context) {
return ListTile(
title: Text("This is a box", style: TextStyle(
decoration: ischanged ? TextDecoration.lineThrough : null
),
),
trailing: Taskcheckbox(ischanged, (bool checkboxState) {
setState(() {
ischanged = checkboxState;
});
}),
);
}
}
class Taskcheckbox extends StatelessWidget {
final bool checkboxState;
final Function toggleCheckboxState;
Taskcheckbox(this.checkboxState, this.toggleCheckboxState);
@override
Widget build(BuildContext context) {
return Checkbox(
activeColor: Colors.lightBlueAccent,
value: checkboxState,
onChanged: toggleCheckboxState,
);
}
}
“`
davy.ai
El mensaje de error
El tipo de argumento 'Function' no se puede asignar al tipo de parámetro 'void Function(bool?)?'
sugiere que hay una incompatibilidad entre el tipo de parámetro esperado y el tipo real de la funcióntoggleCheckboxState
.En el fragmento de código, el widget
Taskcheckbox
recibe como argumentos un valor booleanocheckboxState
y una funcióntoggleCheckboxState
. La propiedadonChanged
del widgetCheckbox
dentro del widgetTaskcheckbox
se establece entoggleCheckboxState
. Sin embargo, la funcióntoggleCheckboxState
que se pasa como argumento aTaskcheckbox
tiene la firma incorrecta. La propiedadonChanged
espera una función que tome un solo argumento booleano y devuelva void, es decir,void Function(bool?)?
.Para solucionar el error, asegúrate de que la función
toggleCheckboxState
tenga la firma correcta. Debe tomar un solo argumento booleano y devolver void. Aquí tienes una versión actualizada del código con la firma corregida:Con este cambio, el código debería compilar sin errores.