NoSuchMethodError says a getter was called on null in Dart 3
Fix it fast
Most common cause, 1 of 2A JSON field is missing or null and the code reads through it as dynamic
Decode into a model class whose fromJson casts each field to its real type.
class Reading {
Reading.fromJson(Map<String, dynamic> json)
: value = json['value'] as int,
unit = json['unit'] as String?;
final int value;
final String? unit;
}
final reading =
Reading.fromJson(jsonDecode(response.body) as Map<String, dynamic>);
Text('${reading.value} ${reading.unit ?? ''}');Short answer
NoSuchMethodError: The getter 'value' was called on null still occurs in Dart 3, but only on a receiver typed dynamic: decoded JSON, an untyped parameter or a raw generic. Typed nullable code fails to compile instead. Enable avoid_dynamic_calls, give the value a type and handle null. Covers Dart 3.10 through 3.14 on the Dart VM.
Which cause do you have?
Ask the analyzer for every member access on a dynamic value. The rule avoid_dynamic_calls lists them all, and the line from your stack trace will be one of the hits; what sits on the left of the dot tells you which cause applies.
# analysis_options.yaml (keep your existing include: line)
linter:
rules:
- avoid_dynamic_calls
flutter analyzeThen find the line you see in this list and open its fix:
- 1A JSON field is missing or null and the code reads through it as dynamic
The flagged line reads from jsonDecode output or a Map<String, dynamic>, and the crash happens for some server responses only - 2A parameter or generic has no type, so Dart filled in dynamic
The flagged receiver is a parameter written without a type, a raw List or Map, or the result of a generic call with no type argument; hovering it in the IDE shows dynamic
What does the error look like?
NoSuchMethodError: The getter 'value' was called on null.Tried calling: valueNoSuchMethodError: The method '[]' was called on null.
Cause 1 of 2
A JSON field is missing or null and the code reads through it as dynamic
You seeThe flagged line reads from jsonDecode output or a Map<String, dynamic>, and the crash happens for some server responses only
Check
The getter was called on null error is common in JSON code because jsonDecode() returns dynamic, so the compiler cannot check what you read from it. If the server drops a key, renames it, or answers with an error body of a different shape, the lookup yields null and the next .value or ['key'] throws at run time. Log the status code and raw body of a failing request to see which field was absent.
debugPrint('${response.statusCode} ${response.body}');Fix
Decode into a model class whose fromJson casts each field to its real type. Every later read is then checked by the compiler. Make a field nullable when the API may leave it out, and read it with ?. or ??.
class Reading {
Reading.fromJson(Map<String, dynamic> json)
: value = json['value'] as int,
unit = json['unit'] as String?;
final int value;
final String? unit;
}
final reading =
Reading.fromJson(jsonDecode(response.body) as Map<String, dynamic>);
Text('${reading.value} ${reading.unit ?? ''}');Verify
flutter analyze no longer flags dynamic calls in this code, and a response without the field now fails inside fromJson, next to the field name, rather than deep inside a widget.
flutter analyzeConfirmed by docs.flutter.dev
Cause 2 of 2
A parameter or generic has no type, so Dart filled in dynamic
You seeThe flagged receiver is a parameter written without a type, a raw List or Map, or the result of a generic call with no type argument; hovering it in the IDE shows dynamic
Check
Sound null safety only guards values that have a static type. A parameter declared with no type, a List or Map without type arguments, and a generic call whose type argument cannot be inferred all end up as dynamic, and a getter read on dynamic skips the null check until run time. Two analyzer language modes, strict-inference and strict-raw-types, report each of these spots.
# analysis_options.yaml
analyzer:
language:
strict-inference: true
strict-raw-types: true
flutter analyzeFix
Write the intended type at every place the analyzer reports. Once the receiver has a nullable type, the compiler refuses an unchecked .value, so you decide there what null should mean.
// before: void show(item) => print(item.value);
void show(Reading? item) => print(item?.value ?? 0);
// before: List readings = [];
final readings = <Reading>[];Verify
The analyzer reports no strict-inference, strict-raw-types or avoid_dynamic_calls findings for the file, and the app no longer throws on the screen that crashed.
flutter analyzeConfirmed by dart.dev
How do I stop it coming back?
- Keep avoid_dynamic_calls and the strict-casts, strict-inference and strict-raw-types modes on in analysis_options.yaml, and run dart analyze --fatal-infos in CI so lint findings fail the build.
- Turn every HTTP or Firestore payload into a model class in one place, right after decoding, and pass only typed models to widgets.
- For more than a handful of models, generate fromJson with json_serializable instead of writing the casts by hand.
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 on the same stack: TaxiDex, Qist Bazaar, SessionBird.