Skip to content

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

Closed

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.

Activity

  1. added
    in triagePresently being triaged by the triage team
    frameworkflutter/packages/flutter repository. See also f: labels.
    c: proposalA detailed proposal for a change to Flutter
    c: new featureNothing broken; request for a new capability
    and removed
    in triagePresently being triaged by the triage team
    on Oct 4, 2021
  2. goderbauer commented on Feb 23, 2022

    @goderbauer
    Member

    As discussed in #91179, this isn't a god idea.

  3. github-actions commented on Mar 10, 2022

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  4. locked as resolved and limited conversation to collaborators on Mar 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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