Dart Functions: Named Parameter

Dart Functions: Named Parameter

ยท

2 min read

in dart named parameters are defined within curly braces { } in a function's parameter list. which means it will be optional that whenever we call function at that it is not mandatory to declare this variable which are in curly backet , by default their value shown as null.

But , if we want to define this value then we must write parameter name before the value to get print of the output otherwise it's throw error.

I have already write both example to learn easily.

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

{
  print("Name = $name");
  print("Roll No = $roll");
  print("Age = $age");

}

void main() {
student("Vinit");

}

//Name = Vinit
//Roll No = null
//Age = null
  • In this we only get value of name in function at the call time , so other both files are null shown in output .

  • While , in the below code we can see that when we call function I have pass all three value , but I have write parameter name before the value for put value on variable .

  • You can see that I have also write required before roll which means I have to declare this roll value in the function at the time of call otherwise I must face error ,or I have to remove requires.


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

{
  print("Name = $name");
  print("Roll No = $roll");
  print("Age = $age");

}

void main() {
student("Vinit" ,roll: 10 , age :20);

}

//Name = Vinit
//Roll No = 10
//Age = 20

Did you find this article valuable?

Support Vinit Mepani (Flutter Developer) by becoming a sponsor. Any amount is appreciated!

ย