Dart Named Parameters
Dart Named Parameters
With named parameters, you write the name of each parameter when you call the function.
This makes the call easier to read, and the order of the arguments does not matter.
To create named parameters, put them inside curly brackets {}.
When you call the function, write the name, a colon : and the value:
Example
void showPerson({required String name, required int age}) {
print('$name is $age years old');
}
void main() {
showPerson(name: 'Tom', age: 30);
showPerson(age: 25, name: 'Anna');
}
Try it Yourself »
Result:
Tom is 30 years old
Anna is 25 years old
The second call sends age first, and it still works, because Dart matches the arguments by name.
The required Keyword
Named parameters are optional by default.
Write required before a named parameter to make it required.
If you leave out a required named argument, the program does not run:
Example
void showPerson({required String name, required int age}) {
print('$name is $age years old');
}
void main() {
showPerson(name: 'Tom');
}
Try it Yourself »
Result:
main.dart:6:13: Error: Required named parameter 'age' must be provided.
showPerson(name: 'Tom');
^
main.dart:1:6: Context: Found this candidate, but the arguments don't match.
void showPerson({required String name, required int age}) {
^^^^^^^^^^
Optional Named Parameters
A named parameter without required is optional.
If it has no default value, it is null when you leave it out, so its type must be nullable:
Example
void sendMessage({required String to, String? subject}) {
print('To: $to');
print('Subject: ${subject ?? '(no subject)'}');
}
void main() {
sendMessage(to: 'Anna', subject: 'Lunch');
sendMessage(to: 'Tom');
}
Try it Yourself »
Result:
To: Anna
Subject: Lunch
To: Tom
Subject: (no subject)
Default Values
Use = to give a named parameter a default value.
The default value is used when the argument is left out:
Example
void order({required String item, int quantity = 1, String size = 'medium'}) {
print('$quantity x $item ($size)');
}
void main() {
order(item: 'coffee');
order(item: 'tea', size: 'large');
order(item: 'pizza', quantity: 3);
}
Try it Yourself »
Result:
1 x coffee (medium)
1 x tea (large)
3 x pizza (medium)
Notice the second call: it sends size but skips quantity.
This is not possible with optional positional parameters, which are always filled from left to right.
Mix Positional and Named Parameters
A function can have positional parameters first, followed by named parameters:
Example
void printPrice(String product, double price, {String currency = 'USD', bool onSale = false}) {
var label = onSale ? ' (on sale)' : '';
print('$product: $price $currency$label');
}
void main() {
printPrice('Shoes', 59.99);
printPrice('Jacket', 120.0, currency: 'EUR');
printPrice('Hat', 15.5, onSale: true);
}
Try it Yourself »
Result:
Shoes: 59.99 USD
Jacket: 120.0 EUR
Hat: 15.5 USD (on sale)
A function cannot have both optional positional parameters [] and named parameters {}.
Pick one of them.
Tip: Use named parameters when a function has many parameters, or when the meaning of a value is not clear from the call.
A call like resize(width: 200, height: 100) is much easier to read than resize(200, 100).
Named parameters are used a lot in Dart and in Flutter, so you will see them often.