Last active
October 24, 2020 11:18
-
-
Save RedBrogdon/15f4ca1103bb4283d335d9f9f9a40e03 to your computer and use it in GitHub Desktop.
Snippet 6: Promotion
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
// With null safety, Dart takes null checks | |
// into account. Nullable variables that | |
// can't possibly contain null are treated | |
// like non-nullable variables. | |
// | |
// This behavior is called "promotion." | |
// | |
// In the example below, add an if-then to | |
// the beginning of `getLength` that | |
// returns zero if `str` is null. | |
int getLength(String? str) { | |
// Add null check here | |
return str.length; | |
} | |
void main() { | |
print(getLength('This is a string!')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello maybe This code should like this
Thanks.