在飞镖:
命名参数的功能如下 -
String send(msg, {rate: 'First Class'}) {
return '${msg} was sent via ${rate}';
}
// you can use named parameters if the argument is optional
send("I'm poor", rate:'4th class'); // == "I'm poor was sent via 4th class"
短手构造函数参数的功能如下 -
class Person {
String name;
// parameters prefixed by 'this.' will assign to
// instance variables automatically
Person(this.name);
}
有没有办法做下面的事情? -
class Person{
String name;
String age;
Person({this.name = "defaultName", this.age = "defaultAge"});
}
//So that I could do something like:
var personAlpha = new Person(name: "Jordan");
谢谢,
借来的代码示例答案 0 :(得分:8)
你只需要使用冒号而不是等于
class Person {
String name;
String age;
Person({this.name = "defaultName", this.age = "defaultAge"});
}
我发现这仍然令人困惑,可选参数使用=
来分配默认值,但命名为use :
。
应该问自己。
答案 1 :(得分:2)
你可以使用"这个。"所有参数类型的语法。 如上所述,您需要':'用于命名参数的默认值。
你甚至可以使用"这个。"对于函数类型参数:
class C {
Function bar;
C({int this.bar(int x) : foo});
static foo(int x) => x + 1;
}