Differentiate between named parameters and positional parameters in Dart? code example

Example: dart positional arguments vs named arguments

void main(){

  var catBob = Cat('Bob', 'Grey');
  
  // display object with positional argument 
  print(catBob.displayCat());
  
  
  // display object with constructor and named arguments
  print(Dog(breed: 'Bulldog', name: 'Greg', color: 'Purple').displayDog());
}



// Cat class - with simple constructor and positional arguments
class Cat{

  // properties
  String name;
  String color;
  
  // simple constructor
  Cat(this.name, this.color);
  
  // method to display cat
  String displayCat() {
    return 'my cat is ${name} and he is ${color}';
  }

}

// Dog class - with constructor and named arguments
class Dog{
  String name;
  String breed;
  String color;
  
  // constructor with named arguments
  Dog({this.name, this.breed, this.color});
  
  // method to display cat
  String displayDog() {
    // interpolate 'breed, name, color' with ${ value }
    return 'my ${breed} is ${name} and he is ${color}';
  }
  
}

Tags:

Dart Example