-
Notifications
You must be signed in to change notification settings - Fork 3.4k
docs: add migration guide for findChildIndexCallback of separated builder #12636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rkishan516
wants to merge
6
commits into
main
Choose a base branch
from
seperated-builder-child-callback
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+160
−0
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9517b90
docs: add migration guide for findChildIndexCallback of seperated bui…
rkishan516 453c654
docs: update findItemIndexCallback example based on gemini feedback
rkishan516 bf66530
docs: update title for migration guide
rkishan516 8dceca3
Update src/content/release/breaking-changes/separated-builder-find-ch…
sfshaza2 3320dde
Update src/content/release/breaking-changes/separated-builder-find-ch…
sfshaza2 8435295
Update src/content/release/breaking-changes/separated-builder-find-ch…
sfshaza2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
...content/release/breaking-changes/separated-builder-find-child-index-callback.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| --- | ||
| title: Deprecate `findChildIndexCallback` in favor of `findItemIndexCallback` in `ListView` and `SliverList` separated constructors | ||
| description: >- | ||
| The findChildIndexCallback parameter in ListView.separated and | ||
| SliverList.separated has been deprecated in favor of findItemIndexCallback. | ||
| --- | ||
|
|
||
| ## Summary | ||
|
|
||
| The `findChildIndexCallback` parameter in `ListView.separated` and | ||
| `SliverList.separated` constructors has been deprecated in favor of | ||
sfshaza2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| `findItemIndexCallback`. The new callback returns item indices directly, | ||
| eliminating the need for manual index calculations to account for separators. | ||
|
|
||
| ## Background | ||
|
|
||
| In `ListView.separated` and `SliverList.separated` constructors, | ||
| the `findChildIndexCallback` was used to locate widgets by their key. | ||
| However, this callback returned child indices, which include both items | ||
| and separators in the internal widget tree. This meant developers had to | ||
sfshaza2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| multiply item indices by 2 to get the correct child index, creating | ||
| confusion and error-prone code. | ||
|
|
||
| The new `findItemIndexCallback` parameter simplifies this by working | ||
| directly with item indices, which do not include separators. | ||
| This makes the API more intuitive and reduces the likelihood of | ||
| index calculation errors. | ||
|
|
||
| If you use the deprecated `findChildIndexCallback` parameter, | ||
| you will see a deprecation warning: | ||
|
|
||
| ``` | ||
| 'findChildIndexCallback' is deprecated and shouldn't be used. | ||
| Use findItemIndexCallback instead. | ||
| findChildIndexCallback returns child indices (which include separators), | ||
| while findItemIndexCallback returns item indices (which do not). | ||
| If you were multiplying results by 2 to account for separators, | ||
| you can remove that workaround when migrating to findItemIndexCallback. | ||
| This feature was deprecated after v3.37.0-1.0.pre. | ||
| ``` | ||
|
|
||
| Additionally, if you try to provide both parameters, you will encounter | ||
| an assertion error: | ||
|
|
||
| ``` | ||
| Cannot provide both findItemIndexCallback and findChildIndexCallback. | ||
| Use findItemIndexCallback as findChildIndexCallback is deprecated. | ||
| ``` | ||
|
|
||
| ## Migration guide | ||
|
|
||
| To migrate from `findChildIndexCallback` to `findItemIndexCallback`, | ||
| replace the parameter name and remove any index multiplications | ||
| that were used to account for separators. | ||
|
|
||
| Code before migration: | ||
|
|
||
| ```dart | ||
| ListView.separated( | ||
| itemCount: items.length, | ||
| findChildIndexCallback: (Key key) { | ||
| final ValueKey<String> valueKey = key as ValueKey<String>; | ||
| final int itemIndex = items.indexOf(valueKey.value); | ||
| // Multiply by 2 to account for separators | ||
| return itemIndex == -1 ? null : itemIndex * 2; | ||
| }, | ||
| itemBuilder: (BuildContext context, int index) { | ||
| return ListTile( | ||
| key: ValueKey<String>(items[index]), | ||
| title: Text(items[index]), | ||
| ); | ||
| }, | ||
| separatorBuilder: (BuildContext context, int index) => const Divider(), | ||
| ) | ||
| ``` | ||
|
|
||
| Code after migration: | ||
|
|
||
| ```dart | ||
| ListView.separated( | ||
| itemCount: items.length, | ||
| findItemIndexCallback: (Key key) { | ||
| final ValueKey<String> valueKey = key as ValueKey<String>; | ||
| final int itemIndex = items.indexOf(valueKey.value); | ||
| // Return item index directly - no need to multiply by 2 | ||
| return itemIndex == -1 ? null : itemIndex; | ||
| }, | ||
| itemBuilder: (BuildContext context, int index) { | ||
| return ListTile( | ||
| key: ValueKey<String>(items[index]), | ||
| title: Text(items[index]), | ||
| ); | ||
| }, | ||
| separatorBuilder: (BuildContext context, int index) => const Divider(), | ||
| ) | ||
| ``` | ||
|
|
||
| The same migration applies to `SliverList.separated`: | ||
|
|
||
| Code before migration: | ||
|
|
||
| ```dart | ||
| SliverList.separated( | ||
| itemCount: items.length, | ||
| findChildIndexCallback: (Key key) { | ||
| final ValueKey<String> valueKey = key as ValueKey<String>; | ||
| final int itemIndex = items.indexOf(valueKey.value); | ||
| return itemIndex == -1 ? null : itemIndex * 2; | ||
| }, | ||
| itemBuilder: (BuildContext context, int index) { | ||
| return Container( | ||
| key: ValueKey<String>(items[index]), | ||
| child: Text(items[index]), | ||
| ); | ||
| }, | ||
| separatorBuilder: (BuildContext context, int index) => const Divider(), | ||
| ) | ||
| ``` | ||
|
|
||
| Code after migration: | ||
|
|
||
| ```dart | ||
| SliverList.separated( | ||
| itemCount: items.length, | ||
| findItemIndexCallback: (Key key) { | ||
| final ValueKey<String> valueKey = key as ValueKey<String>; | ||
| final int itemIndex = items.indexOf(valueKey.value); | ||
| return itemIndex == -1 ? null : itemIndex; | ||
| }, | ||
| itemBuilder: (BuildContext context, int index) { | ||
| return Container( | ||
| key: ValueKey<String>(items[index]), | ||
| child: Text(items[index]), | ||
| ); | ||
| }, | ||
| separatorBuilder: (BuildContext context, int index) => const Divider(), | ||
| ) | ||
| ``` | ||
|
|
||
| ## Timeline | ||
|
|
||
| Landed in version: 3.38.0-1.0.pre<br> | ||
| In stable release: Not yet | ||
|
|
||
| ## References | ||
|
|
||
| API documentation: | ||
|
|
||
| * [`ListView.separated`][] | ||
| * [`SliverList.separated`][] | ||
|
|
||
| Relevant PRs: | ||
|
|
||
| * [Deprecate findChildIndexCallback for separated constructors][] | ||
|
|
||
| [`ListView.separated`]: {{site.api}}/flutter/widgets/ListView/ListView.separated.html | ||
| [`SliverList.separated`]: {{site.api}}/flutter/widgets/SliverList/SliverList.separated.html | ||
| [Deprecate findChildIndexCallback for separated constructors]: {{site.repo.flutter}}/pull/174491 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.