Thuta Learning
IntermediateProgrammingbeginner

Null Safety

Relax. We'll talk through this in plain words — no textbook voice.

Null safety is one of Dart's most important features. By default, it stops a variable from ever becoming null. It catches the 'using a value that doesn't exist' problem — the kind that crashes apps — early.

dart
void main() {
  String name = 'Dart';
  // name = null; // Error: non-nullable variable

  String? nickname;
  nickname = null;

  print(name.length);
  print(nickname?.length ?? 0);
}

String name can never be null. String? is nullable — it can be null. ?. only accesses the property if a value exists, and ?? provides a default value when it's null.

You should see
4 0

Easy traps

  • Avoid using the ! null assertion when you don't need it. value! is basically saying 'trust me, this isn't null' — but if it actually is null, you'll get a runtime error.
Null Safety | Thuta Learning