我是 Flutter 的新手,并试图声明一个文件夹类,其中一个属性是子文件夹列表。我无法得出正确的声明,语言给了我各种错误。有人可以帮我解决这个问题吗?
class Folder {
final int id;
final String title;
final List<Folder> children;
Folder ({
this.id = 0,
this.title = '',
this.children
});
factory Folder.fromJson(Map<String, dynamic> parsedJson) {
Iterable i = parsedJson['children'];
return new Folder(
id: parsedJson['id'] ?? '',
title: parsedJson['title'] ?? '',
children: List<Folder>.from(i.map((model) => Folder.fromJson(model)))
);
}
}
这给我的children属性带来了以下错误:参数“children”的值不能为“null”,因为它的类型,但隐含的默认值是“null”。
有时文件夹没有子文件夹,所以我不想创建一个@required 参数,只是一个可选参数。