# Dart: Bad state: No element — .first, .last or .single on an empty list

**Code:** `Bad state: No element` · **Category:** Flutter / Dart · **Seen on:** Flutter

You called `.first`, `.last`, `.single` or `.reduce` on an empty collection.

## 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

1. Check `list.isNotEmpty` before `.first`
2. Use `firstOrNull` or `firstWhere(test, orElse: () => default)`
3. Show a loading/empty state in the UI

## Detailed fix by platform

### Dart

1. `final first = items.firstOrNull;` (Dart 3 `package:collection` extension in older SDKs)

## How to diagnose

1. **Data** — Empty at that moment?
2. **Getter** — first/last/single/reduce?
3. **Fallback** — orElse supplied?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/flutter/bad-state-no-element)
