Skip to content

Commit dd671fa

Browse files
fix(widget_inspector): add null check for flex factor property to prevent exception (flutter#167890)
Null check exception appears while opening Widget Inspector as the attached screenshots and video. ![null_check_flex_factor](https://github.com/user-attachments/assets/325ee0b6-7c95-40bf-a717-e6eb354f9ad6) https://github.com/user-attachments/assets/a250765a-0bb4-4b45-9451-db4b51347ca2 ## Pre-launch Checklist - [X] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [X] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [X] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [X] I signed the [CLA]. - [X] I listed at least one issue that this PR fixes in the description above. - [X] I updated/added relevant documentation (doc comments with `///`). - [X] I added new tests to check the change I am making, or this PR is [test-exempt]. - [X] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [X] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Ben Konyi <bkonyi@google.com>
1 parent 61fe9b6 commit dd671fa

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

packages/flutter/lib/src/widgets/widget_inspector.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2346,7 +2346,7 @@ mixin WidgetInspectorService {
23462346

23472347
final ParentData? parentData = renderObject.parentData;
23482348
if (parentData is FlexParentData) {
2349-
additionalJson['flexFactor'] = parentData.flex!;
2349+
additionalJson['flexFactor'] = parentData.flex ?? 0;
23502350
additionalJson['flexFit'] = (parentData.fit ?? FlexFit.tight).name;
23512351
} else if (parentData is BoxParentData) {
23522352
final Offset offset = parentData.offset;

packages/flutter/test/widgets/widget_inspector_test.dart

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5137,6 +5137,44 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService {
51375137
},
51385138
);
51395139

5140+
testWidgets('getLayoutExplorerNode omits flexFactor when flex is null', (
5141+
WidgetTester tester,
5142+
) async {
5143+
await tester.pumpWidget(
5144+
const Directionality(
5145+
textDirection: TextDirection.ltr,
5146+
child: Row(
5147+
children: <Widget>[
5148+
Align(
5149+
alignment: Alignment.topLeft,
5150+
child: ColoredBox(
5151+
color: Color(0xFF000000),
5152+
child: SizedBox(width: 14, height: 14),
5153+
),
5154+
),
5155+
],
5156+
),
5157+
),
5158+
);
5159+
5160+
final Element boxElement = tester.element(find.byType(ColoredBox).first);
5161+
service.setSelection(boxElement, group);
5162+
5163+
final String id = service.toId(boxElement, group)!;
5164+
final Map<String, Object?> result =
5165+
(await service.testExtension(
5166+
WidgetInspectorServiceExtensions.getLayoutExplorerNode.name,
5167+
<String, String>{'id': id, 'groupName': group, 'subtreeDepth': '1'},
5168+
))!
5169+
as Map<String, Object?>;
5170+
5171+
final Map<String, Object?>? parentData = result['parentData'] as Map<String, Object?>?;
5172+
expect(parentData, isNotNull);
5173+
expect(parentData!['flexFactor'], isNull);
5174+
expect(parentData['flexFit'], isNull);
5175+
expect(tester.takeException(), isNull);
5176+
});
5177+
51405178
testWidgets('ext.flutter.inspector.getLayoutExplorerNode for RenderBox with FlexParentData', (
51415179
WidgetTester tester,
51425180
) async {
@@ -5180,8 +5218,6 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService {
51805218

51815219
expect(result['flexFactor'], equals(1));
51825220
expect(result['flexFit'], equals('loose'));
5183-
5184-
expect(result['parentData'], isNull);
51855221
});
51865222

51875223
testWidgets('ext.flutter.inspector.getLayoutExplorerNode for RenderView', (

0 commit comments

Comments
 (0)