Maaz Zindani

Loading the index…

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

CustomMultiChildLayout throws that each child must be laid out exactly once

ErrorEach child must be laid out exactly once.

Short answer

This error comes from CustomMultiChildLayout. Its delegate must call layoutChild once for every child on every layout pass. The error means one child was skipped, one was laid out twice, or two children share an id. Read which ids the message names, then fix performLayout.

  • Applies to Flutter 3.35, Dart 3.9
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the full message. It names the delegate and the ids involved. Then list every LayoutId and every layoutChild call side by side.

    grep -rn "LayoutId\|layoutChild\|hasChild" lib/
  2. 2Match what you see, then jump to its fix

    1. 1delegate forgot to lay out the following childperformLayout skips a childFix
    2. 2tried to lay out the child with id ... more than oncelayoutChild is called twice for the same idFix
    3. 3The error appears with a delegate that looks correct, and one child is reported as not laid outTwo children share one LayoutIdFix

What does the error look like?

  • Each child must be laid out exactly once.
  • custom multichild layout delegate tried to lay out the child with id "x" more than once.
  • custom multichild layout delegate forgot to lay out the following child

Cause 1 of 3

performLayout skips a child

1Check

The delegate's performLayout must call layoutChild for each child. A child hidden behind an if, or an id missing from the delegate, is skipped.

grep -n "layoutChild" lib/**/*.dart

2Fix

Call layoutChild for every id you pass to LayoutId. If a child is optional, guard it with hasChild so you never skip one that exists.

@override
void performLayout(Size size) {
  if (hasChild('title')) {
    final s = layoutChild('title', BoxConstraints.loose(size));
    positionChild('title', Offset.zero);
  }
  if (hasChild('body')) {
    layoutChild('body', BoxConstraints.tight(size));
    positionChild('body', Offset.zero);
  }
}

3Verify

Hot restart and open the screen again.

flutter run

Confirmed by api.flutter.dev

Did this fix it?

Cause 2 of 3

layoutChild is called twice for the same id

1Check

A loop or a second measuring step may call layoutChild again for the same id inside one pass.

grep -n "layoutChild" lib/**/*.dart

2Fix

Call layoutChild once per id per pass, keep the returned Size in a variable and reuse it for positioning.

final bodySize = layoutChild('body', BoxConstraints.loose(size));
positionChild('body', Offset(0, size.height - bodySize.height));

3Verify

Run the screen in debug mode and check the console is clean.

flutter run

Confirmed by api.flutter.dev

Did this fix it?

Cause 3 of 3

Two children share one LayoutId

1Check

The id of a LayoutId must be unique among the children of the CustomMultiChildLayout. Children built in a list from the same value can end up with equal ids.

grep -n "LayoutId(" -A2 lib/**/*.dart

2Fix

Give each child its own id, for example an enum value or a string with the index.

children: [
  for (var i = 0; i < items.length; i++)
    LayoutId(id: 'item_$i', child: items[i]),
]

3Verify

Rebuild and confirm the delegate sees one child per id.

flutter run

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Pair every LayoutId with exactly one layoutChild and one positionChild call in the delegate.
  • Use an enum for ids so a typo cannot create a child the delegate does not know.
  • Use hasChild for optional children.

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.