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

← Flutter answers

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.

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. Learn more

Related questions

Start debugging smarter today

Try Bugsee for free — no commitments, no hidden fees.

Free sign upLive demoNo credit card required