Dart Named Parameter

The named parameters are optional parameters referenced by name and declared within curly braces{}.

The named parameters can be utilised in a different order from their declaration.It means when you call  a function, you attach the argument to the label.

parameterName: value.

These are parameters that can be passed in any order by passing the name of the parameter followed by the passed value. For example:

void sum({int num1, int num2});

This function is called like this:

sum(num1: 12, num2: 24);

Also named parameters can also have default values.

 

Example:

Let’s take an example:

void main() {

  student ('Vivek',roll:15,age:50);

}

void student(var name,{var roll, var age}){

  print('Name=$name');

    print('Roll=$roll');

  print('Age=$age');

 

}

 

Output:

Name=Vivek

Roll=15

Age=50.


Advertisements