Skip to content

Flutter runtime errorFlutter: “RangeError (index): Invalid value: Not in inclusive range 0..2: 3”

The screen turns red or grey with RangeError (index): Invalid value: Not in inclusive range 0..2: 3 (newer Dart words it Index out of range: index should be less than 3: 3, and an empty list gives Valid value range is empty: 0). Code asked a list for an item at an index it doesn’t have. The numbers tell you which: the list has 3 items, indexes 0 to 2, and the code asked for index 3.

Short answer

Find the list[index] call in the stack trace. In ListView.builder, set itemCount to the same list you index (itemCount: items.length). In loops, use i < list.length, not <=. Before list[0] or .first, check list.isNotEmpty, or use elementAtOrNull and handle null.

Why the index is out of range, and the fix

  1. 01

    ListView.builder or GridView.builder with the wrong itemCount

    “The itemBuilder callback will be called only with indices greater than or equal to zero and less than itemCount.” Flutter, ListView.builder

    A hard-coded count, or the length of a different list, makes the builder ask for items that don’t exist. Use the length of the exact list the builder reads.

    ListView.builder(
      itemCount: items.length,
      itemBuilder: (context, index) => ItemTile(item: items[index]),
    )
  2. 02

    An off-by-one loop or a fixed index

    “The index must be a valid index of this list, which means that index must be non-negative and less than length.” Dart, List operator []

    for (var i = 0; i <= list.length; i++) runs one step too far, and list[2] fails as soon as the API returns two items. Loop with < or for-in, and read fixed positions only after checking the length.

    for (final item in items) { /* ... */ }
    
    final third = items.length > 2 ? items[2] : null;
  3. 03

    An empty list read before the data arrives

    “Error thrown due to an argument value being outside an accepted range.” Dart, RangeError class

    Valid value range is empty: 0 means the list had no items, often on the first build before a fetch finishes or when a search returns nothing. Show a loading or empty state, or read safely.

    if (items.isEmpty) return const Text('No results');
    final first = items.elementAtOrNull(0);

Checked against the vendor’s docs on : Flutter API: ListView.builder; Dart API: List operator []; Dart API: RangeError.

Rather hand it over?

Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.

Related: Flutter “Bad state: No element” · Flutter “Null check operator used on a null value” · Flutter “Map is not a subtype of type List” · Flutter “type 'Null' is not a subtype of type 'String'”

Questions

More on this error

It happens only after deleting an item. Why?

The list shrank but something still holds the old index, such as a selectedIndex in state or a PageController page. Reset or clamp the stored index when the list changes.

The message says RangeError (end) or (start) instead. Same cause?

Close. That comes from substring or sublist with an end past the string or list length. Clamp the end with min(end, text.length).

Can you fix it for me?

If one bug is the whole problem, the App Fix Ticket covers it: a written diagnosis first, then the fix. If the app has more going on, the Launch Check is a written check of the whole app. Each issue found is rated critical, high, medium or low, with where it is and how it was confirmed. No issues worth fixing, no charge. If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way. This applies to the first 10 Launch Checks bought after October 30, 2026.

A different error?

Describe it and get a written quote

Send the error and what you were doing. You get a written scope, a fixed price and a timeline; Simon replies by email, usually within one business day.