Flutter - 参数类型“void Function()”无法分配给参数类型“void Function(bool?)?”
:错误:参数类型“void Function(dynamic)”无法分配给参数类型“void Function()”。 lib/widgets/tasks_list.dart:25 checkboxCallback: (checkboxState) {
^
: 错误:参数类型“void Function()”无法分配给参数类型“void Function(bool?)?”。 lib/widgets/task_tile.dart:25 onChanged: checkbox回调,
class TasksList extends StatefulWidget {
@override
State<TasksList> createState() => _TasksListState();
}
class _TasksListState extends State<TasksList> {
List<Task> tasks = [
Task(name: 'Buy milk'),
Task(name: 'Buy eggs'),
Task(name: 'Buy bread'),
];
@override
Widget build(BuildContext context) {
return ListView.builder(
itemBuilder: (context, index) {
return TaskTile(
taskTitle: tasks[index].name,
isChecked: tasks[index].isDone,
checkboxCallback: (checkboxState) {
setState(() {
tasks[index].toggleDone();
});
},
);
},
itemCount: tasks.length,
);
}
}
class TaskTile extends StatelessWidget {
final bool? isChecked;
final String? taskTitle;
final VoidCallback checkboxCallback;
TaskTile({
this.isChecked,
this.taskTitle,
required this.checkboxCallback,
});
@override
Widget build(BuildContext context) {
return CheckboxListTile(
title: Text(
taskTitle!,
style: TextStyle(
decoration: isChecked! ? TextDecoration.lineThrough : null,
),
),
activeColor: Colors.lightBlueAccent,
value: isChecked,
onChanged: checkboxCallback,
);
}
}
: Error: The argument type 'void Function(dynamic)' can't be assigned to the parameter type 'void Function()'.
lib/widgets/tasks_list.dart:25
checkboxCallback: (checkboxState) {
^
: Error: The argument type 'void Function()' can't be assigned to the parameter type 'void Function(bool?)?'.
lib/widgets/task_tile.dart:25
onChanged: checkboxCallback,
class TasksList extends StatefulWidget {
@override
State<TasksList> createState() => _TasksListState();
}
class _TasksListState extends State<TasksList> {
List<Task> tasks = [
Task(name: 'Buy milk'),
Task(name: 'Buy eggs'),
Task(name: 'Buy bread'),
];
@override
Widget build(BuildContext context) {
return ListView.builder(
itemBuilder: (context, index) {
return TaskTile(
taskTitle: tasks[index].name,
isChecked: tasks[index].isDone,
checkboxCallback: (checkboxState) {
setState(() {
tasks[index].toggleDone();
});
},
);
},
itemCount: tasks.length,
);
}
}
class TaskTile extends StatelessWidget {
final bool? isChecked;
final String? taskTitle;
final VoidCallback checkboxCallback;
TaskTile({
this.isChecked,
this.taskTitle,
required this.checkboxCallback,
});
@override
Widget build(BuildContext context) {
return CheckboxListTile(
title: Text(
taskTitle!,
style: TextStyle(
decoration: isChecked! ? TextDecoration.lineThrough : null,
),
),
activeColor: Colors.lightBlueAccent,
value: isChecked,
onChanged: checkboxCallback,
);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
VoidCallback 对于没有预期值的回调事件很有用。对于想要将值返回给父级的场景,您将需要使用 Function(x)。
即,
您应该像下面提到的那样更新您的 TaskTile 小部件
VoidCallback is useful for callback events with no expected value. For scenarios where you want to return a value back to the parent, you will want to use Function(x).
ie,
You should update your TaskTile widget like mentioned below
Dart和Flutter空安全发布后,可以使用:
final void Function(bool?) checkboxCallback;
如果您使用 dart-null-safety,这肯定会解决问题。
After the release of Dart and Flutter null safety, you can use:
final void Function (bool?) checkboxCallback;
This will definitely resolve the issue if you're using dart-null-safety.