Skip to content

Commit 1633c47

Browse files
authored
Improve asserts on Element.mount (#153477)
## Description In debugging an issue with mount, I hit one of these asserts, and I thought it would be a much better assert with some context. ## Tests - No test changes because the change is only to the output string of an assert.
1 parent ce63c02 commit 1633c47

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

packages/flutter/lib/src/widgets/framework.dart

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4217,10 +4217,22 @@ abstract class Element extends DiagnosticableTree implements BuildContext {
42174217
/// method, as in `super.mount(parent, newSlot)`.
42184218
@mustCallSuper
42194219
void mount(Element? parent, Object? newSlot) {
4220-
assert(_lifecycleState == _ElementLifecycle.initial);
4221-
assert(_parent == null);
4222-
assert(parent == null || parent._lifecycleState == _ElementLifecycle.active);
4223-
assert(slot == null);
4220+
assert(
4221+
_lifecycleState == _ElementLifecycle.initial,
4222+
'This element is no longer in its initial state (${_lifecycleState.name})',
4223+
);
4224+
assert(
4225+
_parent == null,
4226+
"This element already has a parent ($_parent) and it shouldn't have one yet.",
4227+
);
4228+
assert(
4229+
parent == null || parent._lifecycleState == _ElementLifecycle.active,
4230+
'Parent ($parent) should be null or in the active state (${parent._lifecycleState.name})',
4231+
);
4232+
assert(
4233+
slot == null,
4234+
"This element already has a slot ($slot) and it shouldn't",
4235+
);
42244236
_parent = parent;
42254237
_slot = newSlot;
42264238
_lifecycleState = _ElementLifecycle.active;

0 commit comments

Comments
 (0)