Defining Dart Functions with Positional and Named Parameters
Positional, optional positional, and named parameters explained with ordering rules, trailing commas, Flutter's named style, and Effective Dart API design advice.
Published on • December 3, 2026
AI Assistant

A function’s parameter list is the contract your callers read first, and Dart gives you three shapes of contract: required positional, optional positional, and named. Choosing the right one is half API design.
Required positional parameters
The simplest form: one or more parameters that must be passed, in order, every time.
String greet(String name, String greeting) {
return '$greeting, $name!';
}
greet('Alice', 'Hello'); // 'Hello, Alice!'
Positional arguments are matched purely by position, so the call site reads tersely — but a reader who doesn’t know the signature can’t tell what 'Hello' means. That trade-off drives every rule below. Positional parameters also mean the compiler checks arity for you: pass two arguments where three are required and you get a static error before the code ever runs, and a call that passes a String where an int belongs fails at compile time for the same reason.
Optional positional parameters with square brackets
Wrapping parameters in [] makes them optional, from the right side of the list:
String say(String from, String msg, [String? device]) {
var result = '$from says $msg';
if (device != null) {
result = '$result with a $device';
}
return result;
}
say('Bob', 'Howdy'); // 'Bob says Howdy'
say('Bob', 'Howdy', 'smoke signal'); // 'Bob says Howdy with a smoke signal'
Optional positional parameters without a default value must be nullable, because their implicit default is null. If you give one a default, the type can be non-nullable.
Named parameters with curly braces
Named parameters live in {}. They are all optional unless you mark them required, and callers pass them as name: value:
void enableFlags({bool bold = false, bool hidden = false}) {
print('bold=$bold hidden=$hidden');
}
enableFlags(bold: true); // hidden defaults to false.
Without a default value or required, a named parameter’s type must be nullable. This is why the docs show void enableFlags({bool? bold, bool? hidden}) when there’s no sensible default — the implicit default is null.
The ordering rules
Dart’s grammar is strict about where each kind may appear:
- Any number of required positional parameters come first.
- After that you may have either named parameters or optional positional parameters — never both.
This single rule keeps call sites unambiguous: the compiler never has to guess whether a stray argument belongs to a positional slot or a named one, and readers can pattern-match any signature in the language at a glance.
// Valid
void a(int x, {int y = 0}) {}
void b(int x, [int y = 0]) {}
// Invalid — mixing named and optional positional
void c(int x, [int y = 0], {int z = 0}) {}
| Style | Syntax | Caller writes | Notes |
|---|---|---|---|
| Required positional | f(int a, int b) | f(1, 2) | Order matters, no labels |
| Optional positional | f(int a, [int b]) | f(1) or f(1, 2) | Must trail required positional |
| Named | f(int a, {int b}) | f(1, b: 2) | All optional unless required |
Named arguments can appear anywhere in the call
Dart does not force named arguments to the end — they may be placed anywhere in the argument list when it suits the API. The language tour demonstrates this with a callback first:
repeat(times: 2, () {
print('Hello!');
});
Trailing commas are also welcome — in the call and in the definition. Adding one tells the formatter to explode the arguments onto separate lines, which is why Flutter style uses them liberally. Trailing commas also make future diffs cleaner: adding a new argument touches exactly one line instead of reshuffling the whole list.
Flutter’s named-everything style
Flutter widget constructors are the most visible example of named-only APIs: even mandatory parameters are named.
const Scrollbar({super.key, required Widget child});
Here child is required but still written child: myWidget at the call site. This scales beautifully — a Container might take twenty arguments, and callers only ever pass the handful they care about, in any order they like. Positional-only constructors would be unreadable at that size. Flutter takes the pattern further with named constructors that double as documentation: ListView.builder, ListView.separated, and ListView.custom each read like a sentence describing how the list is built, something impossible to achieve with positional parameters alone.
API design guidance from Effective Dart
Effective Dart’s Design chapter has three rules that map directly onto this choice:
AVOID positional boolean parameters. Booleans are passed literally, so new Task(true) tells the reader nothing. Prefer named arguments, named constructors, or named constants:
// Bad
ListBox(false, true, true);
// Good
ListBox(scroll: true, showScrollbars: true);
Task.repeating();
AVOID optional positional parameters if the user may want to omit earlier parameters. Optional positional arguments should form a logical progression — earlier ones passed more often than later ones. If a caller might skip the first to supply the second, use named parameters instead. The docs contrast String.fromCharCodes(codes, [start, end]) (fine — start almost always precedes end) with Duration({days, hours, minutes}) (named — nobody should pass a “hole”).
CONSIDER omitting the verb for a named boolean parameter. At the call site, caseSensitive: false reads better than matchCase: false when the parameter name already carries the meaning:
RegExp(pattern, caseSensitive: false);
List.from(elements, growable: true);
When each style fits
Pick required positional for one to three parameters that are obvious in meaning and always supplied — substring(start), list.elementAt(3). Use optional positional for a natural tail of parameters that are almost always omitted in order. Default to named for anything with a couple of options, any boolean flag, or any API expected to grow — it’s the most refactor-safe choice, since adding a new named parameter never breaks existing call sites. Remember too that named parameters cost nothing at the definition site but pay off at every call site, where context: and style: beat guessing what the second positional String was supposed to be.
Takeaways
Dart lets you mix required positional parameters with either optional positional or named parameters, but never both kinds of optionality at once. Positional arguments are compact but opaque; named arguments cost a few extra keystrokes and buy clarity, flexibility, and a call site that documents itself.
Takeaway: Prefer named parameters for anything beyond a couple of obvious arguments — they make call sites readable, let booleans explain themselves, and let your API grow without breaking callers.