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 在颤振构造函数中指定默认值_Flutter_Dart - Fatal编程技术网

Flutter 在颤振构造函数中指定默认值

Flutter 在颤振构造函数中指定默认值,flutter,dart,Flutter,Dart,我有一个名为vendor的类 class Vendor { int id; String name; Vendor({this.id , this.name}); } 现在我有了另一个类person,在这个类中我有一个供应商列表,我想在person构造函数中设置默认值: class Person { List<Vendor> vendor; Person({this.vendor}); } 班级人员{ 列出供应商名

我有一个名为vendor的类

class Vendor {
    int id;
    String name;
    
   Vendor({this.id , this.name});
}
现在我有了另一个类person,在这个类中我有一个供应商列表,我想在person构造函数中设置默认值:

class Person {
      List<Vendor> vendor;
   
      Person({this.vendor});
 }
班级人员{
列出供应商名单;
个人({this.vendor});
}
我尝试过这个解决方案,但没有成功

class Person {
       List<Vendor> vendor;
      Person({this.vendor}) : vendor = vendor ?? const [];
 }
班级人员{
列出供应商名单;
Person({this.vendor}):vendor=vendor??const[];
}

您可以使用
=
设置可选构造函数参数的默认值:

class Foo {
  final List<String> strings;
  Foo({this.strings = const ['example']});
}

class Bar {
  final List<String> strings;
  Bar([this.strings = const ['example']]);
}

print(Foo().strings);  // prints '[example]'
print(Bar().strings);  // prints '[example]'
class-Foo{
最终列表字符串;
Foo({this.strings=const['example']});
}
分类栏{
最终列表字符串;
条([this.strings=const['example']]);
}
打印(Foo().strings);//打印“[示例]”
打印(Bar().strings);//打印“[示例]”
注意,作为可选参数传入的值必须是
const