我正在使用开启了空安全的新版Dart <2.13.0-107.0.dev>。
以下是任务列表:
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'),
];
当我尝试在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();
});
},
);
},
);
}
我遇到了这个错误:
错误:函数的返回类型不能为空,但是函数可能会正常完成而返回 'null'。
在运行日志中还出现了这个错误:
错误:由于返回类型 'Widget' 不允许为 null 值,因此必须返回非空值。
更多信息,请参考以下 Task 类的定义:
class Task {
String name;
bool isDone;
Task({this.name = '', this.isDone = false});
void toggleDone() {
isDone = !isDone;
}
}