Optional Named Parameter



Unlike positional parameters, the parameters’ name must be specified while the value is being passed. Curly brace {} can be used to specify optional named parameters.

Syntax - Declaring the function

void function_name(a, {optional_param1, optional_param2}) { } 

Syntax - Calling the function

function_name(optional_param:value,…); 

Example

void main() { 
   test_param(123); 
   test_param(123,s1:'hello'); 
   test_param(123,s2:'hello',s1:'world'); 
}  
test_param(n1,{s1,s2}) { 
   print(n1); 
   print(s1); 
}  

It should produce the following output

123 
null 
123 
hello 
123 
world 
dart_programming_functions.htm
Advertisements