Skip to content

avoid_shrink_wrap_in_lists

v0.4.0 Warning Fix Widget Best Practices

This rule flags ListView widgets that use shrinkWrap: true. When shrink-wrapping is enabled, the ListView lays out all of its children eagerly to determine its own size, which defeats the lazy rendering that makes scrollable lists performant.

The quick fix removes the argument. shrinkWrap defaults to false, so that restores lazy layout without changing anything else — but if the list was shrink-wrapped because it sits inside another scrollable, removing the argument alone will surface an unbounded-height error. That case needs the CustomScrollView restructuring shown below, which is a decision the fix cannot make for you.

A ListView with shrinkWrap: true forces Flutter to measure every single child up front, even the ones that are off-screen. For large lists this is extremely expensive and can cause visible jank or even ANRs. The recommended alternative is to use CustomScrollView with SliverList, which gives you the same nested-scrollable layout without the performance cost.

See also: ScrollView.shrinkWrap | Flutter performance best practices

// ListView with shrinkWrap forces eager layout of all children
final list = ListView(shrinkWrap: true);
final builder = ListView.builder(
shrinkWrap: true,
itemCount: 10,
itemBuilder: (context, index) => Text('$index'),
);
// ListView without shrinkWrap (default lazy rendering)
final list = ListView(children: const [Text('hello')]);
// CustomScrollView with SliverList for nested scroll scenarios
final scroll = CustomScrollView(
slivers: [
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) => Text('$index'),
childCount: 10,
),
),
],
);

This rule is in the opinionated preset, so it is on with preset: opinionated, or by name:

many_lints.yaml
rules:
avoid_shrink_wrap_in_lists: true

To turn it off again:

many_lints.yaml
rules:
avoid_shrink_wrap_in_lists: false

To keep the rule on but skip certain paths, use per-rule exclude.