-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Closed
Labels
c: new featureNothing broken; request for a new capabilityNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to FlutterA detailed proposal for a change to Flutterframeworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.waiting for PR to land (fixed)A fix is in flightA fix is in flight
Description
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.
iapicca, danagbemava-nc, toshi-kuji and stargazing-dino
Metadata
Metadata
Assignees
Labels
c: new featureNothing broken; request for a new capabilityNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to FlutterA detailed proposal for a change to Flutterframeworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.waiting for PR to land (fixed)A fix is in flightA fix is in flight