Created with <3 with dartpad.dev.
Last active
September 21, 2022 09:25
-
-
Save Chralu/021c4321bfc94f0f0418fd6980feca83 to your computer and use it in GitHub Desktop.
Flutter101 - Null aware operators
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
void main() { | |
String? unStringNullable; | |
// KO : Interdiction d'appeler directement une méthode ou | |
// propriété sur un objet nullable. | |
// Ainsi, le langage empêche les [NullPointerException] lors | |
// de l'exécution du code | |
// unStringNullable.length; | |
// OK : L'opérateur `?.` appelle la méthode seulement si | |
// l'objet n'est pas null. | |
// Si l'objet recepteur est null, l'opération retourne null. | |
unStringNullable = "super"; | |
int? length1 = unStringNullable?.length; //length1 vaut 5. | |
unStringNullable = null; | |
int? length2 = unStringNullable?.length; //length2 vaut null. | |
// OK MAIS ATTENTION ! | |
// L'opérateur `!.` permet d'outrepasser les vérifications du compilateur | |
// Cet opérateur dit au compilateur de se comporter comme si | |
// l'objet était non-nullable. | |
// | |
// Remarquez que [lengthNonNull] est du type [int] et non pas [int?] | |
int lengthNonNull = unStringNullable!.length; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment