Dart Named Arguments

There are two types of arguments for a constructor in dart.

  1. Positional arguments – These are commonly used. For Positional arguments you have to remember which argument goes in to which place.
  2. Named arguments – This is a new concept if you belong to C++, Java or PHP. For Named Arguments you don’t need to remember the place/position of the argument. Named Arguments are optional but can be made mandatory using @required
class Person{

  String name="";
  int age=0;
  
  Person({String inputName="", int inputAge=0}){

    this.name = inputName;
    this.age = inputAge;
  }//Person()
  
  Person.veryOld(this.name){
    age = 60;
  }//Person.veryOld()
}//Person


void main() {

  var p1 = Person(inputName: "Rizwan", inputAge: 34);
  var p2 = Person(inputName: "Ali", inputAge: 32);
  var p3 = Person.veryOld("Usama");

  print(p1.name); //Rizwan
  print(p1.age);  //34

  print(p2.name); //Ali
  print(p2.age);  //32

  print(p3.name); //Usama
  print(p3.age);  //60
  
}

Comments

comments

no responses for Dart Named Arguments

    Leave a Reply

    Your email address will not be published. Required fields are marked *