In debug mode the error appears in the console (and as a red screen if it happens during build):
════════ Exception caught by widgets library ════════
The following _TypeError was thrown building ProfileHeader:
Null check operator used on a null value
#0 _ProfileHeaderState.build (package:my_app/profile_header.dart:42:31)
In release builds a widget that fails during build shows as a grey box. Your crash reporter, if you have one, logs the same message.
Find the exact !
Look at the first line of the stack trace that points into your own code (package:your_app/...). The file, line and column point at the ! that hit a null. In this example, profile_header.dart:42:31.
If the trace only shows Flutter or package code, the ! is inside a package. That usually means you called it too early, such as before initialization or before the widget was mounted.
The usual causes and their fixes
1. snapshot.data! in a FutureBuilder or StreamBuilder
The builder runs immediately, before the future completes, so data is null on the first frame.
FutureBuilder<User?>(
future: _userFuture,
builder: (context, snapshot) {
final user = snapshot.data;
if (user == null) return const CircularProgressIndicator();
return Text(user.name); // user is promoted to non-null here
},
)
Check snapshot.hasError too, so an error isn’t shown as an endless spinner. Create the future in initState (_userFuture = loadUser();), not inside build. Otherwise it restarts on every rebuild.
2. _formKey.currentState! or controller.value!
A GlobalKey’s currentState is null until the widget with that key is on screen.
void _submit() {
final form = _formKey.currentState;
if (form == null || !form.validate()) return;
form.save();
}
3. Optional fields from an API or Firebase
Fields like user.avatarUrl, FirebaseAuth.instance.currentUser or ModalRoute.of(context)?.settings.arguments are nullable for a reason. Use ?. and ??:
Text(user.avatarUrl?.split('/').last ?? 'No photo')
When you need the value in several places, copy it to a local variable and check it once. Dart promotes local variables after a null check, but not fields:
final avatar = user.avatarUrl;
avatar != null
? CircleAvatar(backgroundImage: NetworkImage(avatar))
: const CircleAvatar(child: Icon(Icons.person));
4. JSON models with !
json['name']! crashes the day the API leaves a field out. Make the field nullable or give it a default in fromJson. The JSON to Dart converter generates null-safe models with nullable types where your sample JSON has nulls.
When ! is fine
Use ! only when null is truly impossible and you’d want a crash if it ever happened, such as after you’ve just checked it in a way Dart can’t follow. If you need a comment explaining why something can’t be null, it’s often better to restructure the code so the compiler knows too.
Also check for late fields: reading one before it’s assigned throws LateInitializationError, a close cousin of this error with the same kind of fix.
Frequently asked questions
What does "Null check operator used on a null value" mean?
A value was followed by the ! operator, which tells Dart “this is not null”, but at runtime it was null. Dart throws this error at that line. The stack trace points to the exact file and line.
How do I fix snapshot.data! errors in FutureBuilder?
Don’t use ! on snapshot.data. Read it into a local variable, return a loading widget if it is null, and handle snapshot.hasError. Create the future once in initState instead of in build.
Why does my app show a grey screen in release mode?
In release mode, a widget that throws during build is replaced by a grey box instead of the red error screen. A null check error during build is one of the most common causes. Run the same flow in debug mode to see the stack trace.
Should I replace every ! with ??
Not blindly. ?? hides the problem if a fallback makes no sense. Decide what should happen when the value is missing, such as showing a loading state, an empty state or an error, and write that explicitly.
Stuck on a build or a store release? I fix and maintain Flutter apps, or tell me about your project. More fixes: all Flutter answers.