Reputation: 123
can anyone explain why we use the curly bracket in the argument of the constructor.
class Cars {
String carName;
bool isAuto;
// create the constructor
Cars({String honda, bool yes}) {
carName = honda;
isAuto = yes;
}
}
Upvotes: 0
Views: 57
Reputation: 924
Its are named parameters.
For create an instance:
Cars(honda: 'foo', yes: true);
// or
Cars(yes: true, honda: 'foo');
If you don't use curly, will be:
class Cars {
String carName;
bool isAuto;
// create the constructor
Cars(String honda, bool yes) {
carName = honda;
isAuto = yes;
}
}
And then you will create a new instance by order:
Cars('foo', true);
Also, you can initialize automatically:
class Cars {
String carName;
bool isAuto;
Cars(this.carName, this.isAuto);
}
Upvotes: 3