Skip to content

Regression: ListView.builder's itemBuilder can no-longer return null since NNBD migration #108705

@rrousselGit

Description

@rrousselGit

It's a bit late to the party, since NNBD migration was done a long time ago.

But it seems that the migration process broke ListView.builder's itemBuilder ability to return null;

Steps to Reproduce

We used to be able to write the following:

ListView.builder(
  // no item count
  itemBuilder: (context, index) {
    if (condition) return null;
    return Item();
  }
)

Expected results:

Before NNBD migration, this code would render Item() until condition is true, at which point the list ends (similar to as it we reached the end of itemCount)

Actual results:

The code doesn't compile as itemBuilder doesn't allow returning null anymore.


Browsing through the git blames, it seems that this was removed by mistake.
It probably didn't have a test and slipped through the review process because of the NNBD migration of IndexedItemBuilder.

This issue #58371 (comment) in particular shows that returning null was a supported behavior.

Similarly, SliverChildBuilderDelegate.build (the delegate that implements ListView.builder) still allows returning null.
So returning null is still technically supported, just not with the .builder constructor

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work lista: null-safetySupport for Dart's null safety featurec: regressionIt was better in the past than it is nowf: scrollingViewports, list views, slivers, etc.found in release: 3.0Found to occur in 3.0found in release: 3.1Found to occur in 3.1frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer version

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions