Skip to content

Commit 45c517a

Browse files
authored
Fix ListWheelScrollView null check error when it reads itemIndex before content dimensions are set (flutter#194859)
`ListWheelScrollView` throws `Null check operator used on a null value` during its first layout when a child reads the selected item. A common case is an `Image` child. The images then show only after the user scrolls the wheel. The viewport builds its children before it calls `applyContentDimensions`. During this time, an `Image` child resolves its image. This calls `ScrollPosition.recommendDeferredLoading`, which calls `_FixedExtentScrollPosition.copyWith`, which reads `itemIndex`. `itemIndex` reads `minScrollExtent` and `maxScrollExtent`, and these are not set yet. Code that reads `FixedExtentScrollController.selectedItem` during layout has the same problem. This PR changes `_FixedExtentScrollPosition.itemIndex`. When the position has no content dimensions, it returns `(pixels / itemExtent).round()`. This is the same calculation without the clamp to the scroll extents. When the position has content dimensions, the behavior does not change. `FixedExtentMetrics.copyWith` already uses the same `hasContentDimensions` check. Fixes flutter#125997 ### Tests This PR adds regression tests to `packages/flutter/test/widgets/list_wheel_scroll_view_test.dart`: - `ListWheelScrollView can resolve images during its first layout`: a `ListWheelChildBuilderDelegate` with a null `childCount` and `Image` children. The test checks that there is no exception and that the images resolve (`imageCache.liveImageCount` is greater than 0). It clears the image cache when it ends. - `Looping ListWheelScrollView can read selectedItem during its first layout (initialItem: $initialItem)`: a `ListWheelChildLoopingListDelegate` with `initialItem` 7 and -3. Each child reads `selectedItem` during layout. The test checks that all values are equal to `initialItem`. Before this change, the three tests fail: ``` EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE: Null check operator used on a null value EXCEPTION CAUGHT BY WIDGETS LIBRARY: Null check operator used on a null value +55 -3: Some tests failed. ``` After this change, `flutter test test/widgets/list_wheel_scroll_view_test.dart` passes: ``` +58: All tests passed! ``` ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [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. - [ ] I updated/added relevant in-code documentation (doc comments with `///`). No doc change is necessary. - [ ] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). This PR does not add a feature. - [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. - [ ] All existing and new tests are passing. The framework tests pass locally, except tests that also fail on the base commit. The other tests run only in CI. 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 [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/CONTRIBUTING.ai.md [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 [flutter/website]: https://github.com/flutter/website [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
1 parent c0921ab commit 45c517a

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

‎packages/flutter/lib/src/widgets/list_wheel_scroll_view.dart‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,15 @@ class _FixedExtentScrollPosition extends ScrollPositionWithSingleContext
407407

408408
@override
409409
int get itemIndex {
410+
// RenderListWheelViewport applies the content dimensions after it lays out
411+
// its children when the child delegate has no childCount (looping and
412+
// unbounded builder delegates). A child built during that layout, such as
413+
// an Image through ScrollAwareImageProvider, can read this before the
414+
// scroll extents exist. With no extents there is nothing to clamp to, so
415+
// return the item nearest to the current offset.
416+
if (!hasContentDimensions) {
417+
return (pixels / itemExtent).round();
418+
}
410419
return _getItemFromOffset(
411420
offset: pixels,
412421
itemExtent: itemExtent,

‎packages/flutter/test/widgets/list_wheel_scroll_view_test.dart‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'package:flutter/rendering.dart';
1212
import 'package:flutter/widgets.dart';
1313
import 'package:flutter_test/flutter_test.dart';
1414

15+
import '../image_data.dart';
1516
import '../rendering/rendering_tester.dart' show TestCallbackPainter, TestClipPaintingContext;
1617

1718
void main() {
@@ -2059,4 +2060,65 @@ void main() {
20592060
expect(tester.getSize(find.byType(ListWheelViewport)), Size.zero);
20602061
},
20612062
);
2063+
2064+
// Regression test for https://github.com/flutter/flutter/issues/125997.
2065+
testWidgets('ListWheelScrollView can resolve images during its first layout', (
2066+
WidgetTester tester,
2067+
) async {
2068+
addTearDown(imageCache.clear);
2069+
addTearDown(imageCache.clearLiveImages);
2070+
await tester.pumpWidget(
2071+
Directionality(
2072+
textDirection: TextDirection.ltr,
2073+
child: ListWheelScrollView.useDelegate(
2074+
itemExtent: 100.0,
2075+
// A null childCount makes the viewport apply content dimensions after
2076+
// it builds the children.
2077+
childDelegate: ListWheelChildBuilderDelegate(
2078+
builder: (BuildContext context, int index) =>
2079+
Image.memory(Uint8List.fromList(kTransparentImage)),
2080+
),
2081+
),
2082+
),
2083+
);
2084+
expect(tester.takeException(), isNull);
2085+
expect(find.byType(Image), findsWidgets);
2086+
expect(imageCache.liveImageCount, greaterThan(0));
2087+
});
2088+
2089+
// Regression test for https://github.com/flutter/flutter/issues/125997.
2090+
for (final initialItem in <int>[7, -3]) {
2091+
testWidgets(
2092+
'Looping ListWheelScrollView can read selectedItem during its first layout (initialItem: $initialItem)',
2093+
(WidgetTester tester) async {
2094+
final controller = FixedExtentScrollController(initialItem: initialItem);
2095+
addTearDown(controller.dispose);
2096+
final selectedItems = <int>[];
2097+
await tester.pumpWidget(
2098+
Directionality(
2099+
textDirection: TextDirection.ltr,
2100+
child: ListWheelScrollView.useDelegate(
2101+
controller: controller,
2102+
itemExtent: 100.0,
2103+
childDelegate: ListWheelChildLoopingListDelegate(
2104+
children: List<Widget>.generate(
2105+
10,
2106+
(int index) => LayoutBuilder(
2107+
builder: (BuildContext context, BoxConstraints constraints) {
2108+
selectedItems.add(controller.selectedItem);
2109+
return const SizedBox.shrink();
2110+
},
2111+
),
2112+
),
2113+
),
2114+
),
2115+
),
2116+
);
2117+
expect(tester.takeException(), isNull);
2118+
expect(selectedItems, isNotEmpty);
2119+
expect(selectedItems, everyElement(initialItem));
2120+
expect(controller.selectedItem, initialItem);
2121+
},
2122+
);
2123+
}
20622124
}

0 commit comments

Comments
 (0)