Dart parameter default value syntax: colon or equal

Viewed 153

What is the difference between these two syntaxes for setting a default value for a parameter in Dart:

class Test {
  Test({
    int x: 2,
    int y = 3,
  });
}

I tried both of them and they seem to be completely identical.

1 Answers

They're the same. At least for now. The language tour says:

Deprecation note: Old code might use a colon (:) instead of = to set default values of named parameters. The reason is that originally, only : was supported for named parameters. That support might be deprecated, so we recommend that you use = to specify default values.

So avoid using the colon in the future as it may be removed at some point.

Related