Skip to content
Justice Tech School

Optional and Named Parameters

Positional parameters can be optional in square brackets, and named parameters go in curly braces with required or defaults.

Example
String greet(String name, {String punct = "!", required int times}) => "Hello $name" * times + punct;
int sum(int a, [int b = 0]) => a + b;
void main() {
  print(greet("Ada", times: 2));
  print(sum(5));
}

Exercise

Mark a named parameter mandatory.

{ int times}

Quiz Test

Which brackets make named parameters?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.