Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Dart throws ConcurrentModificationError while looping over a list

ErrorException: Concurrent modification during iteration: Instance(length:17) of '_GrowableList'.

Short answer

Dart throws this when a collection changes size while something iterates it. The usual culprits are a remove or add inside a for-in or forEach, and an await inside the loop that lets other code change the list. Iterate a copy, or use a single removeWhere call.

  • Applies to Dart 3.x, Flutter 3.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the stack trace to find the loop, then look at every line inside it for add, remove, insert or clear on the same list.

    grep -rn "\.remove(\|\.removeAt(\|\.add(\|\.insert(\|\.clear(" lib | head -40
  2. 2Match what you see, then jump to its fix

    1. 1The trace points at a for-in loop or a forEach call, and its body calls remove or add on that listThe loop body changes the list it is readingFix
    2. 2The loop contains await, and the error shows up only sometimes, often after a tap or a network eventAn await in the loop lets other code change the listFix

What does the error look like?

  • Exception: Concurrent modification during iteration: Instance(length:17) of '_GrowableList'.
  • ConcurrentModificationError

Cause 1 of 2

The loop body changes the list it is reading

1Check

Changing a list's length between iteration steps causes the error. Look for the list variable on both sides.

grep -n "for (.* in items)\|items.forEach" -A6 lib/cart.dart

2Fix

Remove with one removeWhere call, or loop over a copy and change the original.

items.removeWhere((item) => item.isExpired);

for (final item in items.toList()) {
  if (item.isExpired) items.remove(item);
}

3Verify

Run the screen that failed and repeat the action.

flutter run

Confirmed by api.dart.dev

Did this fix it?

Cause 2 of 2

An await in the loop lets other code change the list

1Check

An await inside the loop lets other code run between two steps. If that code adds to or removes from the list, its length changes between steps, which is what the error reports.

grep -n "await" lib/sync.dart

2Fix

Take a snapshot before the loop and iterate over that.

final snapshot = List.of(queue);
for (final job in snapshot) {
  await upload(job);
  queue.remove(job);
}

3Verify

Trigger the case that used to fail, for example adding items while the loop is running.

flutter run

Confirmed by api.dart.dev

Did this fix it?

How do I stop it coming back?

  • Never add to or remove from a list inside a loop over that same list.
  • Use removeWhere for filtering in place.
  • Copy the list with List.of or toList before an async loop.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.