The getter 'length' was called on null

NoSuchMethodError: The getter 'length' was called on null.

Quick Answer

You tried to access a property on a null object.

Why This Happens

In Dart, accessing a getter like .length on a null reference throws a NoSuchMethodError. This commonly occurs with uninitialized lists or strings. Use null-aware operators or ensure proper initialization before accessing properties.

The Problem

List<String>? items;
print(items.length); // items is null

The Fix

List<String>? items;
print(items?.length ?? 0);

Step-by-Step Fix

  1. 1

    Identify the error

    Look at the error message: The getter 'length' was called on null. This means you accessed a property on a null variable.

  2. 2

    Find the cause

    Check the stack trace to find which variable was null, and determine if it was never initialized or was set to null elsewhere.

  3. 3

    Apply the fix

    Use the ?. operator to safely access the property, or provide a default value with the ?? operator.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free