Can you combine named parameter with short-hand constructor parameter?
You can use the "this." syntax with all argument types. As answered above, you need ':' for default values for named parameters.
You can even use "this." for function typed parameters:
class C {
Function bar;
C({int this.bar(int x) : foo});
static foo(int x) => x + 1;
}
Update
Yes, the =
is allowed in Dart 2 and is now preferred over the :
to match optional positional parameters.
Person({this.name = "defaultName", this.age = "defaultAge"});
Old Answer
You just have to use a colon instead of equals
class Person {
String name;
String age;
Person({this.name: "defaultName", this.age: "defaultAge"});
}
I find this still confusing that optional parameters use =
to assign defaults but named use :
.
Should ask myself.