谁能解释为什么我们在构造函数的参数中使用大括号。
class Cars {
String carName;
bool isAuto;
// create the constructor
Cars({String honda, bool yes}) {
carName = honda;
isAuto = yes;
}
}
答案 0 :(得分:3)
它被命名为参数。
用于创建实例:
Cars(honda: 'foo', yes: true);
// or
Cars(yes: true, honda: 'foo');
如果您不使用卷曲,将是:
class Cars {
String carName;
bool isAuto;
// create the constructor
Cars(String honda, bool yes) {
carName = honda;
isAuto = yes;
}
}
然后您将按顺序创建一个新实例:
Cars('foo', true);
此外,您可以自动初始化:
class Cars {
String carName;
bool isAuto;
Cars(this.carName, this.isAuto);
}