Flutter - 参数类型“void Function()”无法分配给参数类型“void Function(bool?)?”

发布于 2025-01-11 12:49:46 字数 1660 浏览 0 评论 0原文

:错误:参数类型“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兔姬 2025-01-18 12:49:46

VoidCallback 对于没有预期值的回调事件很有用。对于想要将值返回给父级的场景,您将需要使用 Function(x)。

即,

您应该像下面提到的那样更新您的 TaskTile 小部件

class TaskTile extends StatelessWidget {
final bool? isChecked;
final String? taskTitle;

// You should specify the parameter type that you want to pass.
final Function(bool?) 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,
   );
 }
}

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

class TaskTile extends StatelessWidget {
final bool? isChecked;
final String? taskTitle;

// You should specify the parameter type that you want to pass.
final Function(bool?) 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,
   );
 }
}
酷遇一生 2025-01-18 12:49:46

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文