Dart: RangeError (index): Invalid value: Valid value range is empty: 0
A list or string was indexed outside its bounds, often an empty list or an off-by-one in itemBuilder.
RangeError (index): Invalid value: Valid value range is empty: 0RangeError (index): Invalid value: Not in inclusive range 0..2: 3RangeError (end): Invalid valueRangeError (length): Invalid valueUnhandled Exception: RangeError Meaning
list[i] throws RangeError when i < 0 or i >= length. In widgets it usually comes from itemCount not matching the data, mutating a list while building, or reading [0] before data loads.
Common causes
- Index equal to length (off by one)
- Empty list accessed with [0]
- itemCount larger than the list
- List modified between itemCount and itemBuilder
- Substring/sublist ends beyond length
⚡ Quick fix
- Guard with
if (i < list.length) - Set
itemCount: items.lengthfrom the same list - Show an empty state when the list is empty
Detailed fix by platform
Dart
- Use
list.elementAtOrNull(i)(Dart 3) ori >= 0 && i < list.length
How to diagnose
- Index — Value vs length?
- List — Empty at that time?
- itemCount — Matches data?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- Bad state: No element Dart: Bad state: No element — .first, .last or .single on an empty list
- Error: Couldn't resolve the package Dart: Error: Couldn't resolve the package 'x' in 'package:x/x.dart'
- FormatException: Unexpected character Dart: FormatException: Unexpected character (at character 1) <!DOCTYPE html> — jsonDecode of an HTML page
Most viewed in Flutter / Dart
- setState() called after dispose() Flutter: setState() called after dispose(): _MyState#1a2b3 (lifecycle state: defunct, not mounted)
- A RenderFlex overflowed Flutter: A RenderFlex overflowed by x pixels on the right/bottom
- SocketException: Failed host lookup Flutter: Unhandled Exception: SocketException: Failed host lookup: 'api.example.com' (OS Error: No address associated with hostname, errno = 7)
Other ways to find it
Where this error sits
Origin: On your device. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 4 Nov 2026