Skip to content

itemExtentBuilder: too many unnecessary calls #144390

Closed

Description

@antontishkov

Steps to reproduce

itemExtentBuilder is great way to improve app performance. But only when an item height is predefined or easily calculated. But if you need a bit more complex calculations inside the itemExtentBuilder, you will have a performance issue due to many unnecessary itemExtentBuilder calls.

Steps to reproduce the issue.

  1. Use my code example.
  2. Scroll to the item... let it say 45.
  3. Clear your console.
  4. Make two small scrolls.
  5. You will see thousands of itemExtentBuilder calls...

In my case itemExtentBuilder was called 10005 times. And itemBuilder only 1. So the question is... are these 10 thousands calls necessary? And that was just a small scroll on the 45th item. Not on 145th one.

Code sample

Code sample
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';

void main() {
  runApp(
    const MaterialApp(
      title: 'Scroll test',
      home: MyApp(),
      initialRoute: '/',
      // showPerformanceOverlay: true,
    ),
  );
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return const Directionality(
      textDirection: TextDirection.ltr,
      child: DefaultTextStyle(
        textAlign: TextAlign.left,
        style: TextStyle(
          color: Colors.black54,
          fontFamily: 'Roboto',
          fontSize: 13,
          height: 1.4,
        ),
        child: ScrollTestScreen(),
      ),
    );
  }
}

class ScrollTestScreen extends StatefulWidget {
  const ScrollTestScreen({super.key});
  @override
  State<ScrollTestScreen> createState() => _ScrollTestScreenState();
}

class _ScrollTestScreenState extends State<ScrollTestScreen> {
  int _listLength = 9;
  final int _nextPageTrigger = 3;
  final String _text = 'Lorem ipsum dolor sit amet, consectetur adipiscing '
      'elit, sed do eiusmod tempor incididunt ut labore et dolore magna '
      'aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco '
      'laboris nisi ut aliquip ex ea commodo consequat.';
  final _textStyle = const TextStyle(
    color: Colors.black,
    fontSize: 14,
    height: 1.4,
  );
  final _titleStyle = const TextStyle(
    color: Colors.blueAccent,
    fontSize: 15,
    height: 1.4,
  );

  void _addItems() async {
    if (SchedulerBinding.instance.schedulerPhase != SchedulerPhase.idle) {
      await SchedulerBinding.instance.endOfFrame;
    }
    setState(() => _listLength += 5);
  }

  double calcTextHeight(String text, TextStyle style) {
    final textPainter = TextPainter(
      text: TextSpan(text: text, style: style),
      textDirection: TextDirection.ltr,
    )..layout(maxWidth: MediaQuery.of(context).size.width);
    final double height = textPainter.height;
    textPainter.dispose();
    return height;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Column(children: [
        const SizedBox(height: 60),
        const Text('Simple ListView.builder'),
        const SizedBox(height: 20),
        Expanded(
          child: ListView.builder(
            padding: const EdgeInsets.all(0),
            itemCount: _listLength,
            itemBuilder: (_, index) {
              if (index == _listLength - _nextPageTrigger) _addItems();
              print('item $index');
              return ListItem(index, _text, _textStyle, _titleStyle);
            },
            itemExtentBuilder: (index, _) {
              // It is a known bug https://github.com/flutter/flutter/issues/138912
              if (index >= _listLength) {
                //print('out of range');
                return 100;
              }
              print('extent $index');
              return calcTextHeight('Item number 0', _titleStyle) +
                  calcTextHeight(_text, _textStyle) +
                  // Image height and all sized boxes
                  240;
            },
          ),
        ),
      ]),
    );
  }
}

class ListItem extends StatelessWidget {
  const ListItem(
    this.index,
    this.text,
    this.textStyle,
    this.titleStyle, {
    super.key,
  });
  final int index;
  final String text;
  final TextStyle textStyle;
  final TextStyle titleStyle;

  @override
  Widget build(BuildContext context) {
    return Column(children: [
      Container(
        width: double.infinity,
        height: 200,
        color: Colors.blueGrey,
      ),
      const SizedBox(height: 10),
      Text('Item number $index', style: titleStyle),
      const SizedBox(height: 5),
      Text(text, style: textStyle),
      const SizedBox(height: 25),
    ]);
  }
}

Performance profiling on master channel

  • The issue still persists on the master channel

Timeline Traces

N/A

Video demonstration

Video demonstration
2024-02-29.wwv8d.mp4

What target platforms are you seeing this bug on?

iOS

OS/Browser name and version | Device information

iOS simulator: iPhone 15 Pro

Does the problem occur on emulator/simulator as well as on physical devices?

Yes

Is the problem only reproducible with Impeller?

N/A

Logs

No response

Flutter Doctor output

Doctor output
[✓] Flutter (Channel master, 3.18.0-14.0.pre.14, on macOS 14.2.1 23C71 darwin-arm64 (Rosetta), locale
    XX)
    • Flutter version 3.18.0-14.0.pre.14 on channel master at /Users/XXXXX/Development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision bbfe177dc9 (2 months ago), 2023-12-19 05:48:01 -0500
    • Engine revision 1d5a141917
    • Dart version 3.3.0 (build 3.3.0-241.0.dev)
    • DevTools version 2.31.0-dev.0

[✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
    • Android SDK at /Users/Anton/Library/Android/sdk
    • Platform android-32, build-tools 32.1.0-rc1
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15C500b
    • CocoaPods version 1.14.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)

[✓] VS Code (version 1.86.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.82.0

[✓] Connected device (4 available)
    • iPhone XXXXX (mobile)  • 00008110-000A70163682801E            • ios            • iOS 17.3.1
      21D61
    • iPhone 15 Pro (mobile) • B5803A97-0140-4C01-8C0D-6731611F7A73 • ios            •
      com.apple.CoreSimulator.SimRuntime.iOS-17-0 (simulator)
    • macOS (desktop)        • macos                                • darwin-arm64   • macOS 14.2.1
      23C71 darwin-arm64 (Rosetta)
    • Chrome (web)           • chrome                               • web-javascript • Google Chrome
      122.0.6261.94

[✓] Network resources
    • All expected network resources are available.

• No issues found!

Activity

  1. added
    from: performance template
    on Feb 29, 2024
  2. removed
    from: performance template
    on Feb 29, 2024
  3. added
    in triage
    on Mar 1, 2024
  4. darshankawar commented on Mar 1, 2024

    @darshankawar
    Member

    Thanks for the report. I was able to replicate the reported behavior wherein, the console shows the item extent from start, ie, 0 until the last item from which we start to scroll.
    This isn't iOS specific as I verified on Android and seems to be similar as reported.

    stable, master flutter doctor -v
    [!] Flutter (Channel stable, 3.19.1, on macOS 12.2.1 21D62 darwin-x64, locale
        en-GB)
        • Flutter version 3.19.1 on channel stable at
          /Users/dhs/documents/fluttersdk/flutter
        ! Warning: `flutter` on your path resolves to
          /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter, which is not inside
          your current Flutter SDK checkout at
          /Users/dhs/documents/fluttersdk/flutter. Consider adding
          /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path.
        ! Warning: `dart` on your path resolves to
          /Users/dhs/Documents/Fluttersdk/flutter/bin/dart, which is not inside your
          current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter.
          Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front
          of your path.
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision abb292a07e (6 days ago), 2024-02-20 14:35:05 -0800
        • Engine revision 04817c99c9
        • Dart version 3.3.0
        • DevTools version 2.31.1
        • If those were intentional, you can disregard the above warnings; however
          it is recommended to use "git" directly to perform update checks and
          upgrades.
    
    
    [!] Xcode - develop for iOS and macOS (Xcode 12.3)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        ! Flutter recommends a minimum Xcode version of 13.
          Download the latest version or update via the Mac App Store.
        • CocoaPods version 1.11.2
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] VS Code (version 1.62.0)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.21.0
    
    [✓] Connected device (5 available)
        • SM G975F (mobile)       • RZ8M802WY0X • android-arm64   • Android 11 (API 30)
        • Darshan's iphone (mobile)  • 21150b119064aecc249dfcfe05e259197461ce23 •
          ios            • iOS 14.4.1 18D61
        • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729     •
          ios            • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
        • macOS (desktop)            • macos                                    •
          darwin-x64     • Mac OS X 10.15.4 19E2269 darwin-x64
        • Chrome (web)               • chrome                                   •
          web-javascript • Google Chrome 98.0.4758.80
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
    
    ! Doctor found issues in 1 category.
    
    [!] Flutter (Channel master, 3.20.0-16.0.pre.42, on macOS 12.2.1 21D62
        darwin-x64, locale en-GB)
        • Flutter version 3.20.0-16.0.pre.42 on channel master at
          /Users/dhs/documents/fluttersdk/flutter
        ! Warning: `flutter` on your path resolves to
          /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter, which is not inside
          your current Flutter SDK checkout at
          /Users/dhs/documents/fluttersdk/flutter. Consider adding
          /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path.
        ! Warning: `dart` on your path resolves to
          /Users/dhs/Documents/Fluttersdk/flutter/bin/dart, which is not inside your
          current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter.
          Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front
          of your path.
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 7d5dababd0 (88 minutes ago), 2024-02-29 22:55:15 -0500
        • Engine revision 76140bc9ce
        • Dart version 3.4.0 (build 3.4.0-190.0.dev)
        • DevTools version 2.33.1
        • If those were intentional, you can disregard the above warnings; however
          it is recommended to use "git" directly to perform update checks and
          upgrades.
    
    [!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
        • Android SDK at /Users/dhs/Library/Android/sdk
        ✗ cmdline-tools component is missing
          Run `path/to/sdkmanager --install "cmdline-tools;latest"`
          See https://developer.android.com/studio/command-line for more details.
        ✗ Android license status unknown.
          Run `flutter doctor --android-licenses` to accept the SDK licenses.
          See https://flutter.dev/docs/get-started/install/macos#android-setup for
          more details.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 13C100
        • CocoaPods version 1.11.2
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] IntelliJ IDEA Ultimate Edition (version 2021.3.2)
        • IntelliJ at /Applications/IntelliJ IDEA.app
        • Flutter plugin version 65.1.4
        • Dart plugin version 213.7228
    
    [✓] VS Code (version 1.62.0)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.29.0
    
    [✓] Connected device (3 available)
        • Darshan's iphone (mobile) • 21150b119064aecc249dfcfe05e259197461ce23 • ios
          • iOS 15.3.1 19D52
        • macOS (desktop)           • macos                                    •
          darwin-x64     • macOS 12.2.1 21D62 darwin-x64
        • Chrome (web)              • chrome                                   •
          web-javascript • Google Chrome 109.0.5414.119
    
    [✓] Network resources
        • All expected network resources are available.
    
    ! Doctor found issues in 1 category.
          
    [!] Xcode - develop for iOS and macOS (Xcode 12.3)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        ! Flutter recommends a minimum Xcode version of 13.
          Download the latest version or update via the Mac App Store.
        • CocoaPods version 1.11.2
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] VS Code (version 1.62.0)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.21.0
    
    [✓] Connected device (5 available)
        • SM G975F (mobile)       • RZ8M802WY0X • android-arm64   • Android 11 (API 30)
        • Darshan's iphone (mobile)  • 21150b119064aecc249dfcfe05e259197461ce23 •
          ios            • iOS 14.4.1 18D61
        • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729     •
          ios            • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
        • macOS (desktop)            • macos                                    •
          darwin-x64     • Mac OS X 10.15.4 19E2269 darwin-x64
        • Chrome (web)               • chrome                                   •
          web-javascript • Google Chrome 98.0.4758.80
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
    
    ! Doctor found issues in 1 category.
    
    
    
    
  5. added
    framework
    f: scrolling
    has reproducible steps
    team-framework
    found in release: 3.19
    found in release: 3.20
    c: performance
    and removed
    in triage
    on Mar 1, 2024
  6. added
    P3
    triaged-framework
    on Mar 5, 2024
  7. krll-kov commented on Jun 28, 2025

    @krll-kov
    Contributor

    Is there any workaround for this?

  8. github-actions commented on Nov 27, 2026

    @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.

  9. locked as resolved and limited conversation to collaborators on Nov 27, 2026
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

    P3c: performancef: scrollingfound in release: 3.19found in release: 3.20frameworkhas reproducible stepsteam-frameworktriaged-framework

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions