debugAssertIsValid method

  1. @override
bool debugAssertIsValid ({bool isAppliedConstraint: false, InformationCollector informationCollector })
override

Asserts that the constraints are valid.

This might involve checks more detailed than isNormalized.

For example, the BoxConstraints subclass verifies that the constraints are not double.nan.

If the isAppliedConstraint argument is true, then even stricter rules are enforced. This argument is set to true when checking constraints that are about to be applied to a RenderObject during layout, as opposed to constraints that may be further affected by other constraints. For example, the asserts for verifying the validity of RenderConstrainedBox.additionalConstraints do not set this argument, but the asserts for verifying the argument passed to the RenderObject.layout method do.

The informationCollector argument takes an optional callback which is called when an exception is to be thrown. The collected information is then included in the message after the error line.

Returns the same as isNormalized if asserts are disabled.

Implementation

@override
bool debugAssertIsValid({
  bool isAppliedConstraint = false,
  InformationCollector informationCollector,
}) {
  assert(() {
    void throwError(String message) {
      final StringBuffer information = StringBuffer();
      if (informationCollector != null)
        informationCollector(information);
      throw FlutterError('$message\n${information}The offending constraints were:\n  $this');
    }
    if (minWidth.isNaN || maxWidth.isNaN || minHeight.isNaN || maxHeight.isNaN) {
      final List<String> affectedFieldsList = <String>[];
      if (minWidth.isNaN)
        affectedFieldsList.add('minWidth');
      if (maxWidth.isNaN)
        affectedFieldsList.add('maxWidth');
      if (minHeight.isNaN)
        affectedFieldsList.add('minHeight');
      if (maxHeight.isNaN)
        affectedFieldsList.add('maxHeight');
      assert(affectedFieldsList.isNotEmpty);
      if (affectedFieldsList.length > 1)
        affectedFieldsList.add('and ${affectedFieldsList.removeLast()}');
      String whichFields = '';
      if (affectedFieldsList.length > 2) {
        whichFields = affectedFieldsList.join(', ');
      } else if (affectedFieldsList.length == 2) {
        whichFields = affectedFieldsList.join(' ');
      } else {
        whichFields = affectedFieldsList.single;
      }
      throwError('BoxConstraints has ${affectedFieldsList.length == 1 ? 'a NaN value' : 'NaN values' } in $whichFields.');
    }
    if (minWidth < 0.0 && minHeight < 0.0)
      throwError('BoxConstraints has both a negative minimum width and a negative minimum height.');
    if (minWidth < 0.0)
      throwError('BoxConstraints has a negative minimum width.');
    if (minHeight < 0.0)
      throwError('BoxConstraints has a negative minimum height.');
    if (maxWidth < minWidth && maxHeight < minHeight)
      throwError('BoxConstraints has both width and height constraints non-normalized.');
    if (maxWidth < minWidth)
      throwError('BoxConstraints has non-normalized width constraints.');
    if (maxHeight < minHeight)
      throwError('BoxConstraints has non-normalized height constraints.');
    if (isAppliedConstraint) {
      if (minWidth.isInfinite && minHeight.isInfinite)
        throwError('BoxConstraints forces an infinite width and infinite height.');
      if (minWidth.isInfinite)
        throwError('BoxConstraints forces an infinite width.');
      if (minHeight.isInfinite)
        throwError('BoxConstraints forces an infinite height.');
    }
    assert(isNormalized);
    return true;
  }());
  return isNormalized;
}