debugAssertDoesMeetConstraints method
- @override
override
Verify that the object's constraints are being met. Override this function in a subclass to verify that your state matches the constraints object. This function is only called in checked mode and only when needsLayout is false. If the constraints are not met, it should assert or throw an exception.
Implementation
@override
void debugAssertDoesMeetConstraints() {
assert(constraints != null);
assert(() {
if (!hasSize) {
assert(!debugNeedsLayout); // this is called in the size= setter during layout, but in that case we have a size
String contract;
if (sizedByParent)
contract = 'Because this RenderBox has sizedByParent set to true, it must set its size in performResize().\n';
else
contract = 'Because this RenderBox has sizedByParent set to false, it must set its size in performLayout().\n';
throw FlutterError(
'RenderBox did not set its size during layout.\n'
'$contract'
'It appears that this did not happen; layout completed, but the size property is still null.\n'
'The RenderBox in question is:\n'
' $this'
);
}
// verify that the size is not infinite
if (!_size.isFinite) {
final StringBuffer information = StringBuffer();
if (!constraints.hasBoundedWidth) {
RenderBox node = this;
while (!node.constraints.hasBoundedWidth && node.parent is RenderBox)
node = node.parent;
information.writeln('The nearest ancestor providing an unbounded width constraint is:');
information.write(' ');
information.writeln(node.toStringShallow(joiner: '\n '));
}
if (!constraints.hasBoundedHeight) {
RenderBox node = this;
while (!node.constraints.hasBoundedHeight && node.parent is RenderBox)
node = node.parent;
information.writeln('The nearest ancestor providing an unbounded height constraint is:');
information.write(' ');
information.writeln(node.toStringShallow(joiner: '\n '));
}
throw FlutterError(
'$runtimeType object was given an infinite size during layout.\n'
'This probably means that it is a render object that tries to be '
'as big as possible, but it was put inside another render object '
'that allows its children to pick their own size.\n'
'$information'
'The constraints that applied to the $runtimeType were:\n'
' $constraints\n'
'The exact size it was given was:\n'
' $_size\n'
'See https://flutter.io/layout/ for more information.'
);
}
// verify that the size is within the constraints
if (!constraints.isSatisfiedBy(_size)) {
throw FlutterError(
'$runtimeType does not meet its constraints.\n'
'Constraints: $constraints\n'
'Size: $_size\n'
'If you are not writing your own RenderBox subclass, then this is not '
'your fault. Contact support: https://github.com/flutter/flutter/issues/new?template=BUG.md'
);
}
if (debugCheckIntrinsicSizes) {
// verify that the intrinsics are sane
assert(!RenderObject.debugCheckingIntrinsics);
RenderObject.debugCheckingIntrinsics = true;
final StringBuffer failures = StringBuffer();
int failureCount = 0;
double testIntrinsic(double function(double extent), String name, double constraint) {
final double result = function(constraint);
if (result < 0) {
failures.writeln(' * $name($constraint) returned a negative value: $result');
failureCount += 1;
}
if (!result.isFinite) {
failures.writeln(' * $name($constraint) returned a non-finite value: $result');
failureCount += 1;
}
return result;
}
void testIntrinsicsForValues(double getMin(double extent), double getMax(double extent), String name, double constraint) {
final double min = testIntrinsic(getMin, 'getMinIntrinsic$name', constraint);
final double max = testIntrinsic(getMax, 'getMaxIntrinsic$name', constraint);
if (min > max) {
failures.writeln(' * getMinIntrinsic$name($constraint) returned a larger value ($min) than getMaxIntrinsic$name($constraint) ($max)');
failureCount += 1;
}
}
testIntrinsicsForValues(getMinIntrinsicWidth, getMaxIntrinsicWidth, 'Width', double.infinity);
testIntrinsicsForValues(getMinIntrinsicHeight, getMaxIntrinsicHeight, 'Height', double.infinity);
if (constraints.hasBoundedWidth)
testIntrinsicsForValues(getMinIntrinsicWidth, getMaxIntrinsicWidth, 'Width', constraints.maxWidth);
if (constraints.hasBoundedHeight)
testIntrinsicsForValues(getMinIntrinsicHeight, getMaxIntrinsicHeight, 'Height', constraints.maxHeight);
// TODO(ianh): Test that values are internally consistent in more ways than the above.
RenderObject.debugCheckingIntrinsics = false;
if (failures.isNotEmpty) {
assert(failureCount > 0);
throw FlutterError(
'The intrinsic dimension methods of the $runtimeType class returned values that violate the intrinsic protocol contract.\n'
'The following ${failureCount > 1 ? "failures" : "failure"} was detected:\n'
'$failures'
'If you are not writing your own RenderBox subclass, then this is not\n'
'your fault. Contact support: https://github.com/flutter/flutter/issues/new?template=BUG.md'
);
}
}
return true;
}());
}