Growing with the Web

Optional parameters in Dart


Dart has two flavours of optional parameters, positional and named parameters.

Optional positional parameters

Square brackets [] can be used to specify optional positional parameters. If optional positional parameters are defined, function calls may specify a variable number of arguments.

void main() {
  test(1, 2);
  test(1, 2, 3);

void test(a, [b, c]) { }

Optional named parameters

Curly braces {} can be used to specify optional named parameters. These differ to positional parameters in that their names must be specified in order to be passed. Names are specified using the syntax parameter:value

void main() {
  test(1, b:2);
  test(1, b:2, c:3);

  test(1, 2); // compile error

void test(a, {b, c}) { }

Checking if an optional parameter was specified

The syntax ?parameter can be used to test whether an optional parameter was specified.

bool wasOptionalParamSpecified([a]) => ?a;

Note that unspecified parameters are set as null, all the ? syntax actually does is returns whether the parameter equals null as demonstrated by this program.

void main() {
  test();     // false
  test(null); // true
  test(2);    // true

void test([a]) => print(?a);


comments powered by Disqus
Like this article?
Subscribe for more!