Skip to content

Commit 226c483

Browse files
authored
Avoid a few non-null assertions in inspector (#8862)
1 parent 1a7e403 commit 226c483

File tree

10 files changed

+66
-54
lines changed

10 files changed

+66
-54
lines changed

packages/devtools_app/lib/src/screens/inspector/inspector_data_models.dart

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -634,27 +634,29 @@ class FlexLayoutProperties extends LayoutProperties {
634634
size: Size(widths[i], heights[i]),
635635
offset: Offset.zero,
636636
realSize: displayChildren[i].size,
637+
layoutProperties: displayChildren[i],
637638
)
638639
..mainAxisOffset = calculateMainAxisOffset(i)
639-
..crossAxisOffset = calculateCrossAxisOffset(i)
640-
..layoutProperties = displayChildren[i],
640+
..crossAxisOffset = calculateCrossAxisOffset(i),
641641
);
642642
}
643643

644644
final spaces = <RenderProperties>[];
645645
final actualLeadingSpace = leadingSpace(freeSpace);
646646
final actualBetweenSpace = betweenSpace(freeSpace);
647647
final renderPropsWithFullCrossAxisDimension =
648-
RenderProperties(axis: direction)
648+
RenderProperties(
649+
axis: direction,
650+
isFreeSpace: true,
651+
layoutProperties: this,
652+
)
649653
..crossAxisDimension = maxSizeAvailable(crossAxisDirection)
650654
..crossAxisRealDimension = dimension(crossAxisDirection)
651-
..crossAxisOffset = 0.0
652-
..isFreeSpace = true
653-
..layoutProperties = this;
655+
..crossAxisOffset = 0.0;
654656
if (actualLeadingSpace > 0.0 &&
655657
displayMainAxisAlignment != MainAxisAlignment.start) {
656658
spaces.add(
657-
renderPropsWithFullCrossAxisDimension.clone()
659+
renderPropsWithFullCrossAxisDimension.copyWith()
658660
..mainAxisOffset = 0.0
659661
..mainAxisDimension = renderLeadingSpace
660662
..mainAxisRealDimension = actualLeadingSpace,
@@ -664,7 +666,7 @@ class FlexLayoutProperties extends LayoutProperties {
664666
for (var i = 0; i < childrenRenderProps.length - 1; ++i) {
665667
final child = childrenRenderProps[i];
666668
spaces.add(
667-
renderPropsWithFullCrossAxisDimension.clone()
669+
renderPropsWithFullCrossAxisDimension.copyWith()
668670
..mainAxisDimension = renderBetweenSpace
669671
..mainAxisRealDimension = actualBetweenSpace
670672
..mainAxisOffset = child.mainAxisOffset + child.mainAxisDimension,
@@ -674,7 +676,7 @@ class FlexLayoutProperties extends LayoutProperties {
674676
if (actualLeadingSpace > 0.0 &&
675677
displayMainAxisAlignment != MainAxisAlignment.end) {
676678
spaces.add(
677-
renderPropsWithFullCrossAxisDimension.clone()
679+
renderPropsWithFullCrossAxisDimension.copyWith()
678680
..mainAxisOffset =
679681
childrenRenderProps.last.mainAxisDimension +
680682
childrenRenderProps.last.mainAxisOffset
@@ -700,7 +702,7 @@ class FlexLayoutProperties extends LayoutProperties {
700702
}
701703

702704
final renderProperties = childrenRenderProperties[i];
703-
final space = renderProperties.clone()..isFreeSpace = true;
705+
final space = renderProperties.copyWith(isFreeSpace: true);
704706

705707
space.crossAxisRealDimension =
706708
crossAxisDimension - space.crossAxisRealDimension;
@@ -711,9 +713,9 @@ class FlexLayoutProperties extends LayoutProperties {
711713
space.crossAxisDimension *= 0.5;
712714
final crossAxisRealDimension = space.crossAxisRealDimension;
713715
space.crossAxisRealDimension = crossAxisRealDimension * 0.5;
714-
spaces.add(space.clone()..crossAxisOffset = 0.0);
716+
spaces.add(space.copyWith()..crossAxisOffset = 0.0);
715717
spaces.add(
716-
space.clone()
718+
space.copyWith()
717719
..crossAxisOffset =
718720
renderProperties.crossAxisDimension +
719721
renderProperties.crossAxisOffset,
@@ -741,15 +743,15 @@ class FlexLayoutProperties extends LayoutProperties {
741743
static final _textBaselineNamesToValues = TextBaseline.values.asNameMap();
742744
}
743745

744-
/// RenderProperties contains information for rendering a [LayoutProperties] node
746+
/// Information for rendering a [LayoutProperties] node.
745747
class RenderProperties {
746748
RenderProperties({
747749
required this.axis,
750+
required this.layoutProperties,
751+
this.isFreeSpace = false,
748752
Size? size,
749753
Offset? offset,
750754
Size? realSize,
751-
this.layoutProperties,
752-
this.isFreeSpace = false,
753755
}) : width = size?.width ?? 0.0,
754756
height = size?.height ?? 0.0,
755757
realWidth = realSize?.width ?? 0.0,
@@ -759,15 +761,15 @@ class RenderProperties {
759761

760762
final Axis axis;
761763

762-
/// represents which node is rendered for this object.
763-
LayoutProperties? layoutProperties;
764+
/// Represents which node is rendered for this object.
765+
final LayoutProperties layoutProperties;
766+
767+
final bool isFreeSpace;
764768

765769
double dx, dy;
766770
double width, height;
767771
double realWidth, realHeight;
768772

769-
bool isFreeSpace;
770-
771773
Size get size => Size(width, height);
772774

773775
Size get realSize => Size(realWidth, realHeight);
@@ -836,14 +838,14 @@ class RenderProperties {
836838
}
837839
}
838840

839-
RenderProperties clone() {
841+
RenderProperties copyWith({bool? isFreeSpace}) {
840842
return RenderProperties(
841843
axis: axis,
842844
size: size,
843845
offset: offset,
844846
realSize: realSize,
845847
layoutProperties: layoutProperties,
846-
isFreeSpace: isFreeSpace,
848+
isFreeSpace: isFreeSpace ?? this.isFreeSpace,
847849
);
848850
}
849851

packages/devtools_app/lib/src/screens/inspector/layout_explorer/flex/flex.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,8 +590,7 @@ class FlexChildVisualizer extends StatelessWidget {
590590
// TODO(polina-c, jacob314): consider refactoring to remove `!`.
591591
FlexLayoutProperties get root => state.properties!;
592592

593-
// TODO(polina-c, jacob314): consider refactoring to remove `!`.
594-
LayoutProperties get properties => renderProperties.layoutProperties!;
593+
LayoutProperties get properties => renderProperties.layoutProperties;
595594

596595
ObjectGroup? get objectGroup =>
597596
properties.node.objectGroupApi as ObjectGroup?;

packages/devtools_app/lib/src/screens/inspector/layout_explorer/flex/utils.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,9 @@ class AnimatedFlexLayoutProperties
6464
offset: Offset.lerp(beginProps.offset, endProps.offset, t),
6565
size: Size.lerp(beginProps.size, endProps.size, t),
6666
realSize: Size.lerp(beginProps.realSize, endProps.realSize, t),
67-
// TODO(polina-c, jacob314): crnsider refactoring to get rid of `!`.
6867
layoutProperties: AnimatedLayoutProperties(
69-
beginProps.layoutProperties!,
70-
endProps.layoutProperties!,
68+
beginProps.layoutProperties,
69+
endProps.layoutProperties,
7170
animation,
7271
),
7372
),

packages/devtools_app/lib/src/screens/inspector/layout_explorer/ui/free_space.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class FreeSpaceVisualizerWidget extends StatelessWidget {
2323
'h=${toStringAsFixed(renderProperties.realHeight)}';
2424
final widthDescription = 'w=${toStringAsFixed(renderProperties.realWidth)}';
2525
final showWidth =
26-
renderProperties.realWidth != renderProperties.layoutProperties?.width;
26+
renderProperties.realWidth != renderProperties.layoutProperties.width;
2727
final widthWidget = Column(
2828
mainAxisAlignment: MainAxisAlignment.center,
2929
children: [

packages/devtools_app/lib/src/screens/inspector_v2/inspector_data_models.dart

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -728,27 +728,29 @@ class FlexLayoutProperties extends LayoutProperties {
728728
size: Size(widths[i], heights[i]),
729729
offset: Offset.zero,
730730
realSize: displayChildren[i].size,
731+
layoutProperties: displayChildren[i],
731732
)
732733
..mainAxisOffset = calculateMainAxisOffset(i)
733-
..crossAxisOffset = calculateCrossAxisOffset(i)
734-
..layoutProperties = displayChildren[i],
734+
..crossAxisOffset = calculateCrossAxisOffset(i),
735735
);
736736
}
737737

738738
final spaces = <RenderProperties>[];
739739
final actualLeadingSpace = leadingSpace(freeSpace);
740740
final actualBetweenSpace = betweenSpace(freeSpace);
741741
final renderPropsWithFullCrossAxisDimension =
742-
RenderProperties(axis: direction)
742+
RenderProperties(
743+
axis: direction,
744+
isFreeSpace: true,
745+
layoutProperties: this,
746+
)
743747
..crossAxisDimension = maxSizeAvailable(crossAxisDirection)
744748
..crossAxisRealDimension = dimension(crossAxisDirection)
745-
..crossAxisOffset = 0.0
746-
..isFreeSpace = true
747-
..layoutProperties = this;
749+
..crossAxisOffset = 0.0;
748750
if (actualLeadingSpace > 0.0 &&
749751
displayMainAxisAlignment != MainAxisAlignment.start) {
750752
spaces.add(
751-
renderPropsWithFullCrossAxisDimension.clone()
753+
renderPropsWithFullCrossAxisDimension.copyWith()
752754
..mainAxisOffset = 0.0
753755
..mainAxisDimension = renderLeadingSpace
754756
..mainAxisRealDimension = actualLeadingSpace,
@@ -758,7 +760,7 @@ class FlexLayoutProperties extends LayoutProperties {
758760
for (var i = 0; i < childrenRenderProps.length - 1; ++i) {
759761
final child = childrenRenderProps[i];
760762
spaces.add(
761-
renderPropsWithFullCrossAxisDimension.clone()
763+
renderPropsWithFullCrossAxisDimension.copyWith()
762764
..mainAxisDimension = renderBetweenSpace
763765
..mainAxisRealDimension = actualBetweenSpace
764766
..mainAxisOffset = child.mainAxisOffset + child.mainAxisDimension,
@@ -768,7 +770,7 @@ class FlexLayoutProperties extends LayoutProperties {
768770
if (actualLeadingSpace > 0.0 &&
769771
displayMainAxisAlignment != MainAxisAlignment.end) {
770772
spaces.add(
771-
renderPropsWithFullCrossAxisDimension.clone()
773+
renderPropsWithFullCrossAxisDimension.copyWith()
772774
..mainAxisOffset =
773775
childrenRenderProps.last.mainAxisDimension +
774776
childrenRenderProps.last.mainAxisOffset
@@ -794,7 +796,7 @@ class FlexLayoutProperties extends LayoutProperties {
794796
}
795797

796798
final renderProperties = childrenRenderProperties[i];
797-
final space = renderProperties.clone()..isFreeSpace = true;
799+
final space = renderProperties.copyWith(isFreeSpace: true);
798800

799801
space.crossAxisRealDimension =
800802
crossAxisDimension - space.crossAxisRealDimension;
@@ -805,9 +807,9 @@ class FlexLayoutProperties extends LayoutProperties {
805807
space.crossAxisDimension *= 0.5;
806808
final crossAxisRealDimension = space.crossAxisRealDimension;
807809
space.crossAxisRealDimension = crossAxisRealDimension * 0.5;
808-
spaces.add(space.clone()..crossAxisOffset = 0.0);
810+
spaces.add(space.copyWith()..crossAxisOffset = 0.0);
809811
spaces.add(
810-
space.clone()
812+
space.copyWith()
811813
..crossAxisOffset =
812814
renderProperties.crossAxisDimension +
813815
renderProperties.crossAxisOffset,
@@ -835,15 +837,15 @@ class FlexLayoutProperties extends LayoutProperties {
835837
static final _textBaselineNamesToValues = TextBaseline.values.asNameMap();
836838
}
837839

838-
/// RenderProperties contains information for rendering a [LayoutProperties] node
840+
/// Information for rendering a [LayoutProperties] node.
839841
class RenderProperties {
840842
RenderProperties({
841843
required this.axis,
844+
required this.layoutProperties,
845+
this.isFreeSpace = false,
842846
Size? size,
843847
Offset? offset,
844848
Size? realSize,
845-
this.layoutProperties,
846-
this.isFreeSpace = false,
847849
}) : width = size?.width ?? 0.0,
848850
height = size?.height ?? 0.0,
849851
realWidth = realSize?.width ?? 0.0,
@@ -853,15 +855,15 @@ class RenderProperties {
853855

854856
final Axis axis;
855857

856-
/// represents which node is rendered for this object.
857-
LayoutProperties? layoutProperties;
858+
/// Represents which node is rendered for this object.
859+
final LayoutProperties layoutProperties;
860+
861+
final bool isFreeSpace;
858862

859863
double dx, dy;
860864
double width, height;
861865
double realWidth, realHeight;
862866

863-
bool isFreeSpace;
864-
865867
Size get size => Size(width, height);
866868

867869
Size get realSize => Size(realWidth, realHeight);
@@ -930,14 +932,14 @@ class RenderProperties {
930932
}
931933
}
932934

933-
RenderProperties clone() {
935+
RenderProperties copyWith({bool? isFreeSpace}) {
934936
return RenderProperties(
935937
axis: axis,
936938
size: size,
937939
offset: offset,
938940
realSize: realSize,
939941
layoutProperties: layoutProperties,
940-
isFreeSpace: isFreeSpace,
942+
isFreeSpace: isFreeSpace ?? this.isFreeSpace,
941943
);
942944
}
943945

packages/devtools_app/lib/src/screens/inspector_v2/layout_explorer/flex/flex.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,8 +591,7 @@ class FlexChildVisualizer extends StatelessWidget {
591591
// TODO(polina-c, jacob314): consider refactoring to remove `!`.
592592
FlexLayoutProperties get root => state.properties!;
593593

594-
// TODO(polina-c, jacob314): consider refactoring to remove `!`.
595-
LayoutProperties get properties => renderProperties.layoutProperties!;
594+
LayoutProperties get properties => renderProperties.layoutProperties;
596595

597596
ObjectGroup? get objectGroup =>
598597
properties.node.objectGroupApi as ObjectGroup?;

packages/devtools_app/lib/src/screens/inspector_v2/layout_explorer/flex/utils.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,9 @@ class AnimatedFlexLayoutProperties
6464
offset: Offset.lerp(beginProps.offset, endProps.offset, t),
6565
size: Size.lerp(beginProps.size, endProps.size, t),
6666
realSize: Size.lerp(beginProps.realSize, endProps.realSize, t),
67-
// TODO(polina-c, jacob314): crnsider refactoring to get rid of `!`.
6867
layoutProperties: AnimatedLayoutProperties(
69-
beginProps.layoutProperties!,
70-
endProps.layoutProperties!,
68+
beginProps.layoutProperties,
69+
endProps.layoutProperties,
7170
animation,
7271
),
7372
),

packages/devtools_app/lib/src/screens/inspector_v2/layout_explorer/ui/free_space.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class FreeSpaceVisualizerWidget extends StatelessWidget {
2323
'h=${toStringAsFixed(renderProperties.realHeight)}';
2424
final widthDescription = 'w=${toStringAsFixed(renderProperties.realWidth)}';
2525
final showWidth =
26-
renderProperties.realWidth != renderProperties.layoutProperties?.width;
26+
renderProperties.realWidth != renderProperties.layoutProperties.width;
2727
final widthWidget = Column(
2828
mainAxisAlignment: MainAxisAlignment.center,
2929
children: [

packages/devtools_app/test/screens/inspector/layout_explorer/inspector_data_models_test.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,19 +147,22 @@ void main() {
147147
size: const Size(250, 250),
148148
realSize: const Size(50.0, 0.0),
149149
offset: const Offset(0.0, 125.0),
150+
layoutProperties: properties,
150151
),
151152
RenderProperties(
152153
axis: Axis.horizontal,
153154
size: const Size(261.5, 500),
154155
realSize: const Size(75.0, 25.0),
155156
offset: const Offset(250.0, 0.0),
157+
layoutProperties: properties,
156158
),
157159
RenderProperties(
158160
axis: Axis.horizontal,
159161
size: const Size(400, 500),
160162
realSize: const Size(375.0, 25.0),
161163
offset: const Offset(511.5, 0.0),
162164
isFreeSpace: true,
165+
layoutProperties: properties,
163166
),
164167
]);
165168
},
@@ -192,19 +195,22 @@ void main() {
192195
size: const Size(261.5, 500.0),
193196
realSize: const Size(75.0, 25.0),
194197
offset: const Offset(400.0, 0.0),
198+
layoutProperties: properties,
195199
),
196200
RenderProperties(
197201
axis: Axis.horizontal,
198202
size: const Size(250.0, 250.0),
199203
realSize: const Size(50.0, 0.0),
200204
offset: const Offset(661.5, 125.0),
205+
layoutProperties: properties,
201206
),
202207
RenderProperties(
203208
axis: Axis.horizontal,
204209
size: const Size(400, 500),
205210
realSize: const Size(375.0, 25.0),
206211
offset: const Offset(0.0, 0.0),
207212
isFreeSpace: true,
213+
layoutProperties: properties,
208214
),
209215
]);
210216
},

0 commit comments

Comments
 (0)