Skip to content

Instantly share code, notes, and snippets.

@snghnishant
Last active November 26, 2021 13:12
Show Gist options
  • Save snghnishant/1e3a212145b5f1da4406759f2866508f to your computer and use it in GitHub Desktop.
Save snghnishant/1e3a212145b5f1da4406759f2866508f to your computer and use it in GitHub Desktop.
Null Safety Instructions in Dart

Null Safety Instruction

  • In dart unassigned variables are by default assigned null at initialization.
  • In Null safety a variable with a static type should be assigned some value at initialization or if that variable is to be assigned a value later then should be declare with late or made null aware by using ? operator after its type.
  • Statically typed variables declared without null aware (?) operator can never be null in Null safety
  • @required is changed to required in Null Safety
  • We have various operators and annotations in Null Safety such as ?. ?? ??= !. late required

?? this is Null-ish Coalescing operator which assigns a LHS value only if it's not null otherwise it assigns RHS value

  • Eg. a = b ?? c

?. this is null aware access operator which basically tells that value being accessed can be null

  • Eg. someString?.toLowerCas() // will output null if someString is null otherwise gives the result

! Bang operator is to be used if you are sure that the value being accessed is not null

  • Eg. object!["property"]

A good read about null operators in dart

Official guide

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