Skip to content

[Proposal] Add filter to ValueListenableBuilder for controlling rebuilds #91178

@kaboc

Description

@kaboc

Use case

The builder is called every time the valueListenable value changes, therefore if the value is a list and ValueListenableBuilder is used for each single item in ListView, all the items are rebuilt even if only one element of the list changes.

ValueListenableBuilder<List<String>>(
  valueListenable: listNotifier,
  builder: (context, value, child) {
    // Called even when a different element of the list is updated.
    return ListTile(title: Text(value[index].title, ...);
  },
)

Proposal

ValueListenableBuilder will become more convenient with a parameter that is a function to decide if the builder should be called. In the example below, the ListTile is rebuilt only when the relevant element in listNotifier.value is updated, thanks to the filter.

ValueListenableBuilder<List<String>>(
  valueListenable: listNotifier,
  filter: (oldValue, newValue) => newValue[index] != oldValue[index],
  builder: (context, value, child) {
    return ListTile(title: Text(value[index].title, ...);
  },
)

This optimisation is important for the app performance.
The change can also make it easier to use ValueListenableBuilder for state management.

Metadata

Metadata

Assignees

No one assigned

    Labels

    c: new featureNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to Flutterframeworkflutter/packages/flutter repository. See also f: labels.waiting for PR to land (fixed)A fix is in flight

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions