Skip to content

Fix CarouselView.weighted crashes when initlal viewportDimension is 0.0 - #167628

Merged
auto-submit[bot] merged 1 commit into
flutter:masterfrom
bleroux:fix_CarouselView_weighted_crash_on_zero_viewportDimension
Apr 28, 2025
Merged

auto-submit[bot] merged 1 commit into
flutter:masterfrom
bleroux:fix_CarouselView_weighted_crash_on_zero_viewportDimension

Conversation

@bleroux

@bleroux bleroux commented Apr 23, 2025

Copy link
Copy Markdown
Contributor

Description

This PR fixes CarouselView.weigthed crashes due to given constraints being zero (which happen, for instance, when viewportDimension is 0.0).

At startup, a warm-up frame can be produced before the Flutter engine has reported the
initial view metrics. As a result, the first frame can be produced with a size of zero. In the context of CarouselView this leads to some problems mainly related to division by zero.

Similar to #167271 which addressed the same problem for CarouselView.

Related Issue

Fixes #167621

Tests

Adds 3 tests.

@github-actions github-actions Bot added framework flutter/packages/flutter repository. See also f: labels. p: material_ui material_ui package in flutter/packages labels Apr 23, 2025
@bleroux
bleroux requested a review from QuncCccccc April 23, 2025 10:27

@QuncCccccc QuncCccccc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NICE fix! LGTM. Thank you!

// (with weight 7), we leave some space before item 0 assuming there is another
// item -1 as the first visible item.
int get _firstVisibleItemIndex {
if (constraints.viewportMainAxisExtent == 0.0) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a code comment to simply explain why we want to add this check here and below? Something like, If constraints.viewportMainAxisExtent is 0, firstChildExtent will be 0 and cause division error.

@bleroux
bleroux force-pushed the fix_CarouselView_weighted_crash_on_zero_viewportDimension branch from 34d3a36 to 168d2d9 Compare April 28, 2025 07:28
@bleroux bleroux added the autosubmit Merge PR when tree becomes green via auto submit App label Apr 28, 2025
@auto-submit
auto-submit Bot added this pull request to the merge queue Apr 28, 2025
Merged via the queue into flutter:master with commit 0e595cc Apr 28, 2025
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Apr 28, 2025
@bleroux
bleroux deleted the fix_CarouselView_weighted_crash_on_zero_viewportDimension branch April 28, 2025 10:17
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 28, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 28, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 28, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 28, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 28, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 28, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 28, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 28, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 29, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 3, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 3, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 4, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 5, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 5, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 5, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 5, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 5, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 5, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 5, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 5, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 6, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 6, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 6, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 6, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 7, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 7, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 7, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 7, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 7, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 7, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 7, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 14, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 14, 2025
romanejaquez pushed a commit to romanejaquez/flutter that referenced this pull request Aug 14, 2025
….0 (flutter#167628)

## Description

This PR fixes `CarouselView.weigthed` crashes due to given constraints
being zero (which happen, for instance, when viewportDimension is 0.0).
 
At startup, a warm-up frame can be produced before the Flutter engine
has reported the
initial view metrics. As a result, the first frame can be produced with
a size of zero. In the context of CarouselView this leads to some
problems mainly related to division by zero.

Similar to flutter#167271 which
addressed the same problem for `CarouselView`.

## Related Issue

Fixes flutter#167621

## Tests

Adds 3 tests.
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 15, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 15, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

framework flutter/packages/flutter repository. See also f: labels. p: material_ui material_ui package in flutter/packages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CarouselView.weighted throws when given zero constraints

2 participants