Skip to content

Instantly share code, notes, and snippets.

@Chralu
Last active September 19, 2022 14:40
Show Gist options
  • Save Chralu/8e8f0fb2c36103396d3ac09a1aad65e0 to your computer and use it in GitHub Desktop.
Save Chralu/8e8f0fb2c36103396d3ac09a1aad65e0 to your computer and use it in GitHub Desktop.
Flutter101 - Named parameter

Flutter101 - Named parameter

Created with <3 with dartpad.dev.

// 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