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.
Why use this rule
Section titled “Why use this rule”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 childrenfinal 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 scenariosfinal scroll = CustomScrollView( slivers: [ SliverList( delegate: SliverChildBuilderDelegate( (context, index) => Text('$index'), childCount: 10, ), ), ],);Configuration
Section titled “Configuration”This rule is in the opinionated preset, so it is on with
preset: opinionated, or by name:
rules: avoid_shrink_wrap_in_lists: trueTo turn it off again:
rules: avoid_shrink_wrap_in_lists: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”always_pass_global_key— Don’t create a GlobalKey inside build.avoid_conditional_hooks— Never call hooks inside conditionals, loops, or ternaries.avoid_deep_widget_nesting— Keep a widget tree within a nesting budget.avoid_flexible_outside_flex— Only use Flexible and Expanded as direct children of Row, Column, or Flex.