Dart: Bad state: No element — .first, .last or .single on an empty list
You called .first, .last, .single or .reduce on an empty collection.
Bad state: No elementStateError: Bad state: No elementBad state: Too many elementsBad state: Too few elementsUnhandled Exception: Bad state: No element Meaning
These getters throw a StateError when the iterable is empty (or has more than one element for single). Use firstOrNull (Dart 3), isEmpty checks, or firstWhere(..., orElse: ...) to handle empty data, especially for lists filled from the network.
Common causes
- list.first on an empty list
- firstWhere without orElse and no match
- Data not yet loaded when build runs
- single on a list with several items
- reduce on an empty iterable
⚡ Quick fix
- Check
list.isNotEmptybefore.first - Use
firstOrNullorfirstWhere(test, orElse: () => default) - Show a loading/empty state in the UI
Detailed fix by platform
Dart
final first = items.firstOrNull;(Dart 3package:collectionextension in older SDKs)
How to diagnose
- Data — Empty at that moment?
- Getter — first/last/single/reduce?
- Fallback — orElse supplied?
🔧 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: Stream has already been listened to Dart: Bad state: Stream has already been listened to
- 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