Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何在Dart中正确声明类的列表类型属性?_Flutter_Dart - Fatal编程技术网

Flutter 如何在Dart中正确声明类的列表类型属性?

Flutter 如何在Dart中正确声明类的列表类型属性?,flutter,dart,Flutter,Dart,我不熟悉flifter,并且试图声明一个Folder类,其中一个属性是子文件夹列表。我无法得出正确的声明,语言给了我各种错误。有人能帮我吗 class Folder { final int id; final String title; final List<Folder> children; Folder ({ this.id = 0, this.title = '', this.children }); factory Fold

我不熟悉flifter,并且试图声明一个Folder类,其中一个属性是子文件夹列表。我无法得出正确的声明,语言给了我各种错误。有人能帮我吗

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)))
    );
  }
}
类文件夹{
最终int id;
最后的字符串标题;
最后儿童名单;
文件夹({
这个.id=0,
this.title='',
这是我的孩子
});
factory Folder.fromJson(Map parsedJson){
Iterable i=parsedJson['children'];
返回新文件夹(
id:parsedJson['id']??“,
标题:parsedJson['title']??“,
子项:List.from(i.map((model)=>Folder.fromJson(model)))
);
}
}
这为
children
属性提供了以下错误:参数'children'由于其类型而不能具有'null'值,但隐式默认值为'null'


有时文件夹没有子文件夹,所以我不想创建@required参数,只是一个可选参数。

我猜您使用的是启用空安全的最新版本的Dart?
如果是这样,用这种方式声明您的
子项
var

List<Folder>? children;

谢谢@FDuhen,我总是忘记Dart是如何从斯威夫特那里拿走东西的但是,这是可行的,仅声明
this.children=[]
给出的可选参数的默认值必须是常量。它必须是
this.children=const[]
(或者
可以声明children
,或者
children
可以通过初始值设定项列表显式初始化)。
  Folder ({
    this.id = 0,
    this.title = '',
    this.children = []
  });