Skip to content

Instantly share code, notes, and snippets.

@RedBrogdon
Last active October 24, 2020 11:18
Show Gist options
  • Save RedBrogdon/15f4ca1103bb4283d335d9f9f9a40e03 to your computer and use it in GitHub Desktop.
Save RedBrogdon/15f4ca1103bb4283d335d9f9f9a40e03 to your computer and use it in GitHub Desktop.
Snippet 6: Promotion
// 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!'));
}
@Abhishek01039
Copy link

Abhishek01039 commented Oct 24, 2020

Hello maybe This code should like this

int? getLength(String? str) {
  // Add null check here
  
  return str?.length;  
}

void main() {
  print(getLength('This is a string!'));
}

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment