Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Flutter and app store releases

NoSuchMethodError says a getter was called on null in Dart 3

Your error

NoSuchMethodError: The getter 'value' was called on null.2 more lines it can show

Fix it fast

Most common cause, 1 of 2

A 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.

Fix
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 ?? ''}');
Did this fix it?
Full steps for this cause: check, fix, verify

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.

  • Applies to Dart 3.10 to 3.14 (Flutter 3.38 to 3.50.x), Dart VM targets: Android, iOS, desktop
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
# analysis_options.yaml (keep your existing include: line)
linter:
  rules:
    - avoid_dynamic_calls

flutter analyze

Then find the line you see in this list and open its fix:

  1. 1A JSON field is missing or null and the code reads through it as dynamicThe flagged line reads from jsonDecode output or a Map<String, dynamic>, and the crash happens for some server responses only
  2. 2A parameter or generic has no type, so Dart filled in dynamicThe 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: value
  • NoSuchMethodError: 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.

Check
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 ??.

Fix
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.

Verify
flutter analyze

Confirmed by docs.flutter.dev

Did this fix it?

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.

Check
# analysis_options.yaml
analyzer:
  language:
    strict-inference: true
    strict-raw-types: true

flutter analyze

Fix

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.

Fix
// 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.

Verify
flutter analyze

Confirmed by dart.dev

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.