Unverified Commit e2a0d03b authored by Todd Volkert's avatar Todd Volkert Committed by GitHub

Avoid null check operator failure in RenderFlex._hasOverflow (#72122)

Fixes https://github.com/flutter/flutter/issues/71972
parent bb0e6ce9
......@@ -483,10 +483,10 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
}
// Set during layout if overflow occurred on the main axis.
double? _overflow;
double _overflow = 0;
// Check whether any meaningful overflow is present. Values below an epsilon
// are treated as not overflowing.
bool get _hasOverflow => _overflow! > precisionErrorTolerance;
bool get _hasOverflow => _overflow > precisionErrorTolerance;
/// {@macro flutter.material.Material.clipBehavior}
///
......@@ -1125,10 +1125,10 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
final Rect overflowChildRect;
switch (_direction) {
case Axis.horizontal:
overflowChildRect = Rect.fromLTWH(0.0, 0.0, size.width + _overflow!, 0.0);
overflowChildRect = Rect.fromLTWH(0.0, 0.0, size.width + _overflow, 0.0);
break;
case Axis.vertical:
overflowChildRect = Rect.fromLTWH(0.0, 0.0, 0.0, size.height + _overflow!);
overflowChildRect = Rect.fromLTWH(0.0, 0.0, 0.0, size.height + _overflow);
break;
}
paintOverflowIndicator(context, offset, Offset.zero & size, overflowChildRect, overflowHints: debugOverflowHints);
......@@ -1144,7 +1144,7 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
@override
String toStringShort() {
String header = super.toStringShort();
if (_overflow != null && _hasOverflow)
if (_hasOverflow)
header += ' OVERFLOWING';
return header;
}
......
......@@ -632,4 +632,20 @@ void main() {
expect(() => flex.getMaxIntrinsicWidth(100), cannotCalculateIntrinsics);
expect(() => flex.getMinIntrinsicWidth(100), cannotCalculateIntrinsics);
});
test('Can call methods that check overflow even if overflow value is not set', () {
final List<dynamic> exceptions = <dynamic>[];
final RenderFlex flex = RenderFlex(children: const <RenderBox>[]);
// This forces a check for _hasOverflow
expect(flex.toStringShort(), isNot(contains('OVERFLOWING')));
layout(flex, phase: EnginePhase.paint, onErrors: () {
exceptions.addAll(renderer.takeAllFlutterExceptions());
});
// We expect the RenderFlex to throw during performLayout() for not having
// a text direction, thus leaving it with a null overflow value. It'll then
// try to paint(), which also checks _hasOverflow, and it should be able to
// do so without an ancillary error.
expect(exceptions, hasLength(1));
expect(exceptions.first.message, isNot(contains('Null check operator')));
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment