Back to Articles

Beginner

A Deep Dive into Null Safety in Flutter

· 6 min read

What Is Null Safety?

Dart's sound null safety makes whether a variable can be null part of the type system itself. If a type is covered by null safety, the compiler guarantees that null can never be assigned to it — and that guarantee holds at runtime too. As a result, most runtime null errors like 'Null check operator used on a null value' are now caught at compile time, right in your editor, with a red squiggly line.

Nullable vs Non-Nullable Types

In Dart, every type is non-nullable by default: write String name; and name can never be null — you have to assign a value before using it. To say a variable may be null, you add a ? to the end of the type: String? lastName; That small mark tells the compiler you must check for null before using the value.

String name = 'Muhammet';   // non-nullable: can never be null
String? lastName;           // nullable: starts null, can be assigned later

void greet(String name) {   // name can't be null, no check needed
  print('Hello $name');
}

Null-Aware Operators

Dart gives you a few operators for working safely with nullable values: • ?? : uses the right-hand value if the left side is null (a default value). • ??= : assigns the right-hand value only if the variable is currently null. • ?. : if the object is null, the whole expression evaluates to null instead of calling the method. • ?.. (cascade) : continues the chained calls if the object isn't null, and does nothing if it is.

String? lastName;

final fullName = 'Muhammet ${lastName ?? 'Maraslı'}'; // default with ??
lastName ??= 'Maraslı';                                 // assign if null
final length = lastName?.length;                        // null if lastName is null

Type Promotion and late

After a null check, Dart automatically promotes the variable's type to non-nullable — this is called type promotion. Inside if (user != null), user is now typed as User, not User? — you don't need to add ! again. The late keyword is for a different scenario: it says the variable is non-nullable, but its value will be assigned later, not in the constructor. In Flutter this is typically used for controllers created in initState.

void printUser(User? user) {
  if (user != null) {
    print(user.name); // type promotion: user is now User here
  }
}

class _PageState extends State<Page> {
  late final AnimationController _controller; // assigned in initState

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(vsync: this, duration: const Duration(milliseconds: 300));
  }
}

The ! Operator: When Is It Safe?

The ! (null assertion) operator tells the compiler 'trust me, this isn't null, I won't check.' If the value actually is null at that point, the app crashes: Null check operator used on a null value. ! is only safe when you're 100% certain the value can't be null — for example, right after you've checked a Map for a key yourself. When you're not sure, using ?? or if (x != null) instead turns a potential crash into something the compiler can help you avoid.

final params = {'id': '42'};

// Risky: even if you're sure params['name'] exists, the compiler doesn't know
final name = params['name']!; // crashes here if params['name'] is null

// Safer
final safeName = params['name'] ?? 'unknown';

Things to Watch Out For

1. Overusing !

Slapping ! everywhere just to silence a compiler warning erases most of the benefit of null safety. The error just moves back to runtime — the only difference is you now get a line number telling you exactly where it blew up. Before adding !, ask yourself: can this value really never be null?

2. Reading a late Field Before It's Assigned

If you try to read a field declared with late before it has been assigned, you get 'LateInitializationError: Field has not been initialized.' This is especially common with controllers assigned conditionally inside initState — make sure the controller is created unconditionally, right at the top of initState.

Conclusion

Null safety is one of Dart's most fundamental language features, and used correctly it catches the vast majority of null errors at compile time. Mark which values can be empty explicitly with ?, use ?? and ?. for safe access, reserve ! for places you're genuinely certain about, and only reach for late when the value is guaranteed to be assigned.