dart optional parameters
// Dart Optional Positional Parameters
void myFunction(String name1, String name2, [String name3]) {
// ...name3 is optional, and after required parameters
}
// Dart Optional Named Parameters
void myFunction(int x, {int y = 2}) {
// ...y is optional, default is 2
}
myFunction(2, y: 4); // sequence does not matter
4
8
Curly brackets {} are used to specify optional, named parameters in Dart.
readFile(String name, {String mode, String charset = 'utf-8'}) {
// empty
}
Named parameters are referenced by name,
which means that they can be used during the function
invocation in an order different from the function declaration.
readFile('hello.dart');
readFile('hello.dart', mode: 'w+');
readFile('hello.dart', charset: 'iso8859-1');
readFile('hello.dart', charset: 'iso8859-1', mode: 'w+');
readFile('hello.dart', mode: 'w+', charset: 'iso8859-1');
Thank you!
8
0
Are there any code examples left?
New code examples in category Dart
-
Dart 2022-03-27 22:05:31 how to create a toast in flutter
-
Dart 2022-03-27 21:10:09 dart string empty or null
-
Dart 2022-03-27 16:20:02 golang radom array
-
Dart 2022-03-25 08:00:34 flutter outline button overlay
-
Dart 2022-03-24 23:50:20 sort list dart
-
Dart 2022-03-24 21:00:01 how to obfuscate flutter code
-
Dart 2022-03-22 13:45:18 box decoration s flutter
-
Dart 2022-03-22 07:20:04 New Year's Eve
-
Dart 2022-03-17 17:10:05 string to int in dart
-
Dart 2022-03-17 12:40:14 dart how to tell if an object is an instance of a class