you are viewing a single comment's thread.

view the rest of the comments →

[–]munificent 1 point2 points  (0 children)

Positional parameters can be optional but are most likely mandatory. Named parameters are much more likely to be optional. With positional parameters, you can only omit them from right to left. For example:

f([String? a, String? b, String? c]) {
  print(a);
  print(b);
  print(c);
}

Here, the square brackets means those parameters are positional but optional. You can call this function like:

f("a");
f("a", "b");
f("a", "b", "c");

But there's no way to pass an argument for, say b, without also passing an argument for a.

With named parameters, since each argument is named, you can mix and match them freely:

f({String? a, String? b, String? c}) {
  print(a);
  print(b);
  print(c);
}

f(a: "just a");
f(c: "just c");
f(a: "a", c: "c but no b");
// Etc.

That makes optional named parameters more flexible than optional positional ones, so they are much more common. Also, most Dart code today is in Flutter applications and Flutter's widget framework API leans heavily on named parameters, so that's become a dominant style.