Flutter: Null check operator used on a null value
Null check operator used on a null valueShort answer
The exclamation mark tells Dart a value is not null and throws if it is. The error means a value you asserted was still null at that moment. Find the line in the stack trace, then check the value first, give it a fallback, or wait for the data before reading it.
Fix it fast
1Find out which cause you have
Open the first stack trace line that points at your own code in lib/. The ! on that line is the one that failed.
flutter run --verbose2Match what you see, then jump to its fix
- 1
The error appears while a screen first draws, before the data has loadedData is read before it has arrivedFix - 2
The ! sits on a map lookup, a lookup result or an optional fieldA nullable value is asserted when null is a normal caseFix - 3
The failing line reads key.currentState! or key.currentContext!A GlobalKey has no state yetFix - 4
The code has if (_value != null) and then _value! further downA field is asserted after you checked itFix
- 1
What does the error look like?
Null check operator used on a null value
Cause 1 of 4
Data is read before it has arrived
1Check
Futures and streams deliver their data later. Until then the value is null, and a FutureBuilder starts with null data unless you set initialData.
grep -rn "snapshot\.data!" lib/2Fix
Test for data before using it and show something else while waiting.
builder: (context, snapshot) {
final data = snapshot.data;
if (data == null) {
return const CircularProgressIndicator();
}
return Text(data.title);
},3Verify
Restart the app; the spinner shows first and the error is gone.
flutter runConfirmed by api.flutter.dev
Cause 2 of 4
A nullable value is asserted when null is a normal case
1Check
The ! operator always throws when the value is null. If null can legitimately happen, the assertion turns a normal case into a crash.
grep -rn "[a-zA-Z_)\]]!\." lib/ | head -302Fix
Use ?. to skip the call, or ?? to give a fallback value.
final label = user?.name ?? 'Guest';3Verify
The code runs with a null value and shows the fallback.
flutter testConfirmed by dart.dev
Cause 3 of 4
A GlobalKey has no state yet
1Check
currentState is null when no widget with that key is in the tree, or when that widget has no State of the expected type.
grep -rn "currentState!" lib/2Fix
Make sure the keyed widget is built before you use the key, and read the state with a null check.
final form = _formKey.currentState;
if (form != null && form.validate()) {
// submit
}3Verify
Tap the button before and after the form is visible; no exception is thrown.
flutter runConfirmed by api.flutter.dev
Cause 4 of 4
A field is asserted after you checked it
1Check
Dart narrows local variables after a null check, but not fields that can change. Another piece of code may set the field to null in between.
grep -rn "!= null" lib/ | head -202Fix
Copy the field into a local variable and test that.
final value = _value;
if (value != null) {
print(value.length);
}3Verify
Run the same path; no assertion is needed and none is thrown.
dart analyzeConfirmed by dart.dev
How do I stop it coming back?
- Treat every ! in a code review as a question: can this value really be null?
- Use initialData or a loading state for every FutureBuilder and StreamBuilder.
- Use a nullable type in your models for fields that the server may leave out.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Qist BazaarA buy-now-pay-later marketplace built from a blank repo, now past 100K downloads on Google Play.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Mobile apps
AudioCache has no play method after updating audioplayers
The method 'play' isn't defined for the type 'AudioCache'.
Since audioplayers 1.0, AudioCache only turns asset files into local files and caches them.
2 causes, each with a fixMobile apps
Flutter Bloc throws that emit was called after the event handler completed
emit was called after an event handler completed normally.
Each on<Event> handler gets its own Emitter, and it stops working the moment the handler's returned Future completes.
2 causes, each with a fixMobile apps
analysis_options.yaml: the URI it includes can't be found
include_file_not_found
The analyzer reports include_file_not_found when the file named after include: in analysis_options.yaml does not resolve.
2 causes, each with a fixMobile apps
Dart error: the body might complete normally and return null for a non-nullable type
body_might_complete_normally
Dart found a path through your function or callback that reaches the closing brace without a return.
3 causes, each with a fixMobile apps
Dart throws ConcurrentModificationError while looping over a list
ConcurrentModificationError
Dart throws this when a collection changes size while something iterates it.
2 causes, each with a fixMobile apps
Dart cannot turn a compact timestamp like 20201101153000 into a DateTime
FormatException: Invalid date format
DateTime.parse accepts a subset of ISO 8601, and a time part must be introduced by T or a space.
2 causes, each with a fix