CustomMultiChildLayout throws that each child must be laid out exactly once
Each 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.
Fix it fast
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/2Match what you see, then jump to its fix
- 1
delegate forgot to lay out the following childperformLayout skips a childFix - 2
tried to lay out the child with id ... more than oncelayoutChild is called twice for the same idFix - 3
The error appears with a delegate that looks correct, and one child is reported as not laid outTwo children share one LayoutIdFix
- 1
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/**/*.dart2Fix
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 runConfirmed by api.flutter.dev
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/**/*.dart2Fix
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 runConfirmed by api.flutter.dev
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/**/*.dart2Fix
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 runConfirmed by raw.githubusercontent.com
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Qist BazaarA buy-now-pay-later marketplace built from a blank repo, now past 100K downloads on Google Play.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Mobile apps
AudioCache has no play method after updating audioplayers
The method 'play' isn't defined for the type 'AudioCache'.
Since audioplayers 1.0, AudioCache only turns asset files into local files and caches them.
2 causes, each with a fixMobile apps
Flutter Bloc throws that emit was called after the event handler completed
emit was called after an event handler completed normally.
Each on<Event> handler gets its own Emitter, and it stops working the moment the handler's returned Future completes.
2 causes, each with a fixMobile apps
analysis_options.yaml: the URI it includes can't be found
include_file_not_found
The analyzer reports include_file_not_found when the file named after include: in analysis_options.yaml does not resolve.
2 causes, each with a fixMobile apps
Dart error: the body might complete normally and return null for a non-nullable type
body_might_complete_normally
Dart found a path through your function or callback that reaches the closing brace without a return.
3 causes, each with a fixMobile apps
Dart throws ConcurrentModificationError while looping over a list
ConcurrentModificationError
Dart throws this when a collection changes size while something iterates it.
2 causes, each with a fixMobile apps
Dart cannot turn a compact timestamp like 20201101153000 into a DateTime
FormatException: Invalid date format
DateTime.parse accepts a subset of ISO 8601, and a time part must be introduced by T or a space.
2 causes, each with a fix