Skip to content

Instantly share code, notes, and snippets.

@RedBrogdon
Last active July 12, 2021 21:17
Show Gist options
  • Save RedBrogdon/3e53f7b92a90a1b5a31484453e76160e to your computer and use it in GitHub Desktop.
Save RedBrogdon/3e53f7b92a90a1b5a31484453e76160e to your computer and use it in GitHub Desktop.
Snippet 8: The assertion operator
// If you'd like to assign a nullable expression
// to a variable that's non-nullable, you can use
// the assertion operator (the exclamation point:
// `!`). By adding `!` just after the expression,
// you tell Dart that the value won't be null,
// and it's safe to assign to a non-nullable
// variable.
//
// Note: if you're wrong, an exception will be
// thrown!
//
// Try adding exclamation points to correct the
// three broken assignments in this code:
int? couldReturnNullButDoesnt() => -3;
void main() {
int? couldBeNullButIsnt = 1;
List<int?> listThatCouldHoldNulls = [2, null, 4];
int a = couldBeNullButIsnt;
int b = listThatCouldHoldNulls.first;
int c = couldReturnNullButDoesnt().abs();
print('a is $a.');
print('b is $b.');
print('c is $c.');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment