debugAssertIsValid method

  1. @override
bool debugAssertIsValid(
  1. {bool isAppliedConstraint = false,
  2. 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(DiagnosticsNode message) {
      throw FlutterError.fromParts(<DiagnosticsNode>[
        message,
        if (informationCollector != null) ...informationCollector(),
        DiagnosticsProperty<BoxConstraints>('The offending constraints were', this, style: DiagnosticsTreeStyle.errorProperty),
      ]);
    }
    if (minWidth.isNaN || maxWidth.isNaN || minHeight.isNaN || maxHeight.isNaN) {
      final List<String> affectedFieldsList = <String>[
        if (minWidth.isNaN) 'minWidth',
        if (maxWidth.isNaN) 'maxWidth',
        if (minHeight.isNaN) 'minHeight',
        if (maxHeight.isNaN) '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(ErrorSummary('BoxConstraints has ${affectedFieldsList.length == 1 ? 'a NaN value' : 'NaN values' } in $whichFields.'));
    }
    if (minWidth < 0.0 && minHeight < 0.0) {
      throwError(ErrorSummary('BoxConstraints has both a negative minimum width and a negative minimum height.'));
    }
    if (minWidth < 0.0) {
      throwError(ErrorSummary('BoxConstraints has a negative minimum width.'));
    }
    if (minHeight < 0.0) {
      throwError(ErrorSummary('BoxConstraints has a negative minimum height.'));
    }
    if (maxWidth < minWidth && maxHeight < minHeight) {
      throwError(ErrorSummary('BoxConstraints has both width and height constraints non-normalized.'));
    }
    if (maxWidth < minWidth) {
      throwError(ErrorSummary('BoxConstraints has non-normalized width constraints.'));
    }
    if (maxHeight < minHeight) {
      throwError(ErrorSummary('BoxConstraints has non-normalized height constraints.'));
    }
    if (isAppliedConstraint) {
      if (minWidth.isInfinite && minHeight.isInfinite) {
        throwError(ErrorSummary('BoxConstraints forces an infinite width and infinite height.'));
      }
      if (minWidth.isInfinite) {
        throwError(ErrorSummary('BoxConstraints forces an infinite width.'));
      }
      if (minHeight.isInfinite) {
        throwError(ErrorSummary('BoxConstraints forces an infinite height.'));
      }
    }
    assert(isNormalized);
    return true;
  }());
  return isNormalized;
}