Repository navigation
Commit 45c517a
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.md1 parent c0921ab commit 45c517a
2 files changed
Lines changed: 71 additions & 0 deletions
File tree
- packages/flutter
- lib/src/widgets
- test/widgets
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
407 | 407 | | |
408 | 408 | | |
409 | 409 | | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
410 | 419 | | |
411 | 420 | | |
412 | 421 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| |||
2059 | 2060 | | |
2060 | 2061 | | |
2061 | 2062 | | |
| 2063 | + | |
| 2064 | + | |
| 2065 | + | |
| 2066 | + | |
| 2067 | + | |
| 2068 | + | |
| 2069 | + | |
| 2070 | + | |
| 2071 | + | |
| 2072 | + | |
| 2073 | + | |
| 2074 | + | |
| 2075 | + | |
| 2076 | + | |
| 2077 | + | |
| 2078 | + | |
| 2079 | + | |
| 2080 | + | |
| 2081 | + | |
| 2082 | + | |
| 2083 | + | |
| 2084 | + | |
| 2085 | + | |
| 2086 | + | |
| 2087 | + | |
| 2088 | + | |
| 2089 | + | |
| 2090 | + | |
| 2091 | + | |
| 2092 | + | |
| 2093 | + | |
| 2094 | + | |
| 2095 | + | |
| 2096 | + | |
| 2097 | + | |
| 2098 | + | |
| 2099 | + | |
| 2100 | + | |
| 2101 | + | |
| 2102 | + | |
| 2103 | + | |
| 2104 | + | |
| 2105 | + | |
| 2106 | + | |
| 2107 | + | |
| 2108 | + | |
| 2109 | + | |
| 2110 | + | |
| 2111 | + | |
| 2112 | + | |
| 2113 | + | |
| 2114 | + | |
| 2115 | + | |
| 2116 | + | |
| 2117 | + | |
| 2118 | + | |
| 2119 | + | |
| 2120 | + | |
| 2121 | + | |
| 2122 | + | |
| 2123 | + | |
2062 | 2124 | | |
0 commit comments