Optional parameters
Optional parameters can be either named or positional, but not both.
Named parameters
When calling a function, you can specify named parameters usingparamName: value. For example:
1. enableFlags(bold: true, hidden: false);
When defining a function, use{param1, param2, …}to specify named parameters:
1. /// Sets the [bold] and [hidden] flags ...
2. void enableFlags({bool bold, bool hidden}) {...}
Although named parameters are a kind of optional parameter,you can annotate them with @required to indicatethat the parameter is mandatory —that users must provide a value for the parameter.For example:
1. const Scrollbar({Key key, @required Widget child})
If someone tries to create a Scrollbarwithout specifying the child argument,then the analyzer reports an issue.
To use the @required annotation,depend on the meta package and import package:meta/meta.dart.
Positional parameters
Wrapping a set of function parameters in [] marks them as optionalpositional parameters:
1. String say(String from, String msg, [String device]) {
2. var result = '$from says $msg';
3. if (device != null) {
4. result = '$result with a $device';
5. }
6. return result;
7. }
Here’s an example of calling this function without the optionalparameter:
1. assert(say('Bob', 'Howdy') == 'Bob says Howdy');
And here’s an example of calling this function with the third parameter:
1. assert(say('Bob', 'Howdy', 'smoke signal') ==
2. 'Bob says Howdy with a smoke signal');
Default parameter values
Your function can use = to define default values for both named and positionalparameters. The default values must be compile-time constants.If no default value is provided, the default value is null.
Here’s an example of setting default values for named parameters:
1. /// Sets the [bold] and [hidden] flags ...
2. void enableFlags({bool bold = false, bool hidden = false}) {...}
4. // bold will be true; hidden will be false.
5. enableFlags(bold: true);
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.
The next example shows how to set default values for positional parameters:
1. String say(String from, String msg,
2. [String device = 'carrier pigeon', String mood]) {
3. var result = '$from says $msg';
4. if (device != null) {
5. result = '$result with a $device';
6. }
7. if (mood != null) {
8. result = '$result (in a $mood mood)';
9. }
10. return result;
11. }
13. assert(say('Bob', 'Howdy') ==
14. 'Bob says Howdy with a carrier pigeon');
You can also pass lists or maps as default values.The following example defines a function, doStuff(),that specifies a default list for the listparameter and a default map for the gifts parameter.
1. void doStuff(
2. {List<int> list = const [1, 2, 3],
3. Map<String, String> gifts = const {
4. 'first': 'paper',
5. 'second': 'cotton',
6. 'third': 'leather'
7. }}) {
8. print('list: $list');
9. print('gifts: $gifts');
10. }
