Dart Parameters
Dart Parameters
Information can be passed to a function as parameters.
You write the parameters inside the parentheses, after the function name.
Each parameter has a type and a name.
This function has one parameter, called name, of type String:
Example
void greet(String name) {
print('Hello, $name!');
}
void main() {
greet('Tom');
greet('Anna');
greet('Linus');
}
Try it Yourself »
Result:
Hello, Tom!
Hello, Anna!
Hello, Linus!
Note: The variable in the function definition is called a parameter.
The value you send when you call the function is called an argument.
In the example above, name is a parameter, and 'Tom' is an argument.
Multiple Parameters
You can have as many parameters as you want, separated by commas:
Example
void showPerson(String name, int age) {
print('$name is $age years old');
}
void main() {
showPerson('Tom', 30);
showPerson('Anna', 25);
}
Try it Yourself »
Result:
Tom is 30 years old
Anna is 25 years old
These are called positional parameters, because the arguments are matched to the parameters by their position.
The first argument goes to the first parameter, the second argument goes to the second parameter, and so on.
Required Parameters
Normal positional parameters are required.
You must send exactly one argument for each of them, with the right type.
If you send too few arguments, the program does not run:
Example
void showPerson(String name, int age) {
print('$name is $age years old');
}
void main() {
showPerson('Tom');
}
Try it Yourself »
Result:
main.dart:6:13: Error: Too few positional arguments: 2 required, 1 given.
showPerson('Tom');
^
main.dart:1:6: Context: Found this candidate, but the arguments don't match.
void showPerson(String name, int age) {
^^^^^^^^^^
The type is checked too, so you cannot send a String where an int is expected:
Example
void showPerson(String name, int age) {
print('$name is $age years old');
}
void main() {
showPerson('Tom', 'thirty');
}
Try it Yourself »
Result:
main.dart:6:21: Error: The argument type 'String' can't be assigned to the parameter type 'int'.
showPerson('Tom', 'thirty');
^
Optional Positional Parameters
To make a positional parameter optional, put it inside square brackets [].
Optional parameters must come after the required ones.
An optional parameter that you do not send is null, so its type must be nullable, like String?:
Example
void greet(String name, [String? title]) {
if (title != null) {
print('Hello, $title $name!');
} else {
print('Hello, $name!');
}
}
void main() {
greet('Smith', 'Dr.');
greet('Anna');
}
Try it Yourself »
Result:
Hello, Dr. Smith!
Hello, Anna!
Default Parameter Values
You can give an optional parameter a default value with =.
The default value is used when no argument is sent.
Because the parameter always has a value, its type does not need to be nullable:
Example
void myCountry([String country = 'Norway']) {
print('I am from $country');
}
void main() {
myCountry('Sweden');
myCountry('India');
myCountry();
myCountry('Brazil');
}
Try it Yourself »
Result:
I am from Sweden
I am from India
I am from Norway
I am from Brazil
Here is a function with one required parameter and two optional parameters with default values:
Example
void order(String item, [int quantity = 1, String size = 'medium']) {
print('$quantity x $item ($size)');
}
void main() {
order('coffee');
order('tea', 2);
order('pizza', 3, 'large');
}
Try it Yourself »
Result:
1 x coffee (medium)
2 x tea (medium)
3 x pizza (large)
Optional positional parameters are filled from left to right.
You cannot skip quantity and only send size.
When you want to send only some of the optional values, use named parameters instead.
Note: A default value must be a constant, like a number, a string or true.