---
title: "How do you fix \"Null check operator used on a null value\" in Flutter?"
description: "A ! operator hit a null. Replace it with ?, ??, an if check or a local variable, and look for values read before they are loaded."
url: "https://bugsee.com/answers/flutter/fix-null-check-operator-used-on-a-null-value/"
---

# How do you fix "Null check operator used on a null value" in Flutter?

Somewhere code used `!` on a value that was null at that moment. Replace the bang with a null check, a default using `??`, or fix the order in which the value gets set.

```dart
String? title;

final a = title!.length;          // throws if title is null

final b = title?.length;          // int?, null when title is null
final c = title?.length ?? 0;     // default instead

if (title != null) {
  print(title.length);            // promoted to non-null inside the check
}
```

- The stack trace points at the line with the `!`; start there.
- Typical sources are data that has not loaded yet, Navigator or Theme lookups with `context.findAncestor...` after the widget is disposed, and late-initialised fields used too early.
- Dart only promotes local variables. Copy a field into a local (`final t = title;`) before checking it.

Crashes like this depend on a particular state. Bugsee records the session before a Flutter error so you can see how the app got there. https://bugsee.com/flutter-performance-monitoring/

## Related questions

- [How do you fix "setState() called after dispose()" in Flutter?](https://bugsee.com/answers/flutter/fix-setstate-called-after-dispose/)
