我正在使用新的dart 版本<2.13.0-107.0.开发>启用空安全.

使用此任务列表:

  List<Task> tasks = [
    Task(name: 'find a way to live happy'),
    Task(name: 'Listen to music!!!'),
    Task(name: 'Live in the other world till ur power is off'),
  ];

当我try 在ListView.builder构造函数中使用它时,如下所示:

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: tasks.length,
      itemBuilder: (context, index) {
         TaskTile(
          taskTitle: tasks[index].name,
          isChecked: tasks[index].isDone,
          checkboxCallback: (bool? checkboxState) {
            setState(() {

              tasks[index].toggleDone();
            });
          },
        );
      },
    );
  }

我得到这个错误:

100

运行日志(log)中出现以下错误:

100

有关详细信息,任务类定义如下:

class Task {
  String name;
  bool isDone;

  Task({this.name = '', this.isDone = false});

  void toggleDone() {
    isDone = !isDone;
  }
}

推荐答案

你忘了在你的itemBuilder里用return.

使用

ListView.builder(
  itemBuilder: (context, index) {
    return TaskTile(...); // <-- 'return' was missing 
  },
)

Dart相关问答推荐

基本数学函数的DART源代码

如何在 Dart 2.17+ 中处理future 的枚举值

您如何在元素中设置自定义元素标签的样式?

Flutter 错误:主体可能正常完成,导致返回null,但返回类型可能是不可为空的类型

什么时候在 Dart 中使用interfaces接口?

InheritedWidget - 在 navigator.push 之后在 null 上调用 getter

如何处理 ListView 滚动方向

Dart 中使用 ifAbsent 的 Map Update 方法

如何使用我的 dart 包私有而不在 pub dart lang 上显示?

不要将 PageView 居中 - Flutter

如何在 Flutter CustomPainter 中使用Bezier Curves绘制形状

通常由 TextField 创建的 InputDecorator 不能具有无限宽度

如何在flatter中读取本地json导入?

dart:web_gl: 渲染警告:texture bound to texture unit 0 is not renderable

Flutter:主题未应用于文本小部件

是否有必要为Flatter安装Android Studio,或者可以用什么替代Android Studio?

为什么 Angular 在组件名称中不需要破折号

将函数/方法分配给 Dart 中的变量

Dart 的服务器端框架

如何在 Dart 中进行整数除法?