Saturday, May 5, 2018

Named optional parameter vs Positional Optional Parameter in Dart




Name Positional Optional Parameters

main(List<String> arguments) {
Person person1 = new Person('John', age: 26, address: 'NY');
Person person2 = new Person('Lily', isMale: false, address: 'CA');
print('${person1.name} is ${person1.age} years old and he/she is living in ${person1.address}');
print('${person2.name} is ${person2.age} years old and he/she is living in ${person2.address}');
}
class Person {
// [] positional optional params
// {} named optional params
Person(this.name, {this.age = 100, this.address = 'nowhere', this.isMale = true, this.isYoung = true});
final bool isYoung;
final bool isMale;
final String name;
final int age;
final String address;
}
void printName({String name = 'No name'}) {
print(name);
}
void printData([String name = 'No name', int age = 100]) {
print('$name $age' );
}

No comments:

Post a Comment