Dart Foundations

Null safety and types

A nullable type such as String? tells the compiler that the value may be null. This moves many mistakes from runtime into compile-time feedback.

dart
String? readName() => null;

void greet() {
  final name = readName();
  if (name != null) {
    print('Hello $name');
  }
}
  • Use ?. for conditional member access.
  • Use ?? for a fallback.
  • Use ! only when a non-null invariant is genuinely established.
  • Use late for values initialized after object construction, but avoid it when normal initialization is clearer.
Lesson complete?Use the next lesson to continue the learning path.