【发布时间】:2021-06-26 15:16:03
【问题描述】:
我正在使用启用了 null 安全性的新 dart 版本 。
有了这个任务列表:
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”,但返回类型可能是不可为 null 的类型。
运行日志中出现此错误:
错误:必须返回非空值,因为返回类型“Widget”不允许为空。
更多信息,Task 类定义如下:
class Task {
String name;
bool isDone;
Task({this.name = '', this.isDone = false});
void toggleDone() {
isDone = !isDone;
}
}
【问题讨论】:
标签: flutter dart flutter-listview dart-null-safety