Created with <3 with dartpad.dev.
Last active
September 19, 2022 14:40
-
-
Save Chralu/8e8f0fb2c36103396d3ac09a1aad65e0 to your computer and use it in GitHub Desktop.
Flutter101 - Named parameter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) 2022, TheTribe.io | |
String helloWithNamedParameters({ | |
required String name, // les paramètres obligatoires sont préfixés de `required` | |
required int? age, // un paramètres peut être obligatoires ET nullable | |
double? size, // paramètre nullable et facultatif | |
}) { | |
if (size != null) { | |
return "Bonjour $name ($age ans, $size mètres)"; | |
} | |
return "Bonjour $name ($age ans)"; | |
} | |
void main () { | |
// OK | |
print( | |
helloWithNamedParameters( | |
name: "John", | |
age: 20, | |
size: 1.8, | |
), | |
); | |
// OK | |
print( | |
helloWithNamedParameters( | |
name: "John", | |
age: 20, | |
size: null, | |
), | |
); | |
// OK : ordre des parametres non fixé | |
print( | |
helloWithNamedParameters( | |
age: 20, | |
size: null, | |
name: "John", | |
), | |
); | |
// OK | |
// `size` est facultatif (non `required`) | |
print( | |
helloWithNamedParameters( | |
name: "John", | |
age: 20, | |
), | |
); | |
// KO : ne compilerait pas | |
// `age` est obligatoire | |
// print( | |
// helloWithNamedParameters( | |
// name: "John", | |
// ), | |
// ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment