Skip to content

ListView scrolling exception when item is a dynamic height web image #143817

Closed as not planned

Description

@fyxtc

Steps to reproduce

The listview loads the web image, and when I scroll anywhere below (index10 in the video) and then scroll up, it scrolls right back to the beginning.Found it on my ios 16.5 and 17.3 devices at first, then realized it's the same on the dartpad.

If I use a fixed height it's fine, but I have no way of knowing the image height. If I use cacheExtent: maxFinite it's also fine, but the number of images is potentially so large that I can't use it and it will cause too much memory causing the app to crash!

I think this is a very serious problem, it's too much of an impact on the user experience

Expected results

I've used cacheExtent:1000, which shouldn't scroll back to the beginning, but should smoothly scroll to where it should be

Actual results

Scroll to the beginning

Code sample

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

void main() => runApp(const CupertinoApp(home: TestListView2()));

final testUrls = List.generate(
    24,
    (index) =>
        "https://s2.baozicdn.com/scomic/fanpaidashixiongshimeimenquanshibingjiao-pikapi/0/1-i8qs/${index + 1}.jpg");

class TestListView2 extends StatelessWidget {
  const TestListView2({super.key});

  @override
  Widget build(BuildContext context) {
 return ListView.builder(
      cacheExtent: 1000,
      itemCount: testUrls.length,
      itemBuilder: (context, index) {
        return Container(
          color: Colors.white,
          child: Image.network(
            testUrls[index],
            loadingBuilder: (context, child, loadingProgress) {
              if (loadingProgress == null) {
                return Column(
                  children: [
                    child,
                    Text("index $index"),
                  ],
                );
              } else {
                return SizedBox(child: Text("loading...$index"));
              }
            },
          ),
        );
      },
    );
  }
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

f2-output.mp4

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.19.0, on macOS 13.6.1 22G313 darwin-x64, locale zh-Hans-CN)
[!] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    ✗ 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 15.0.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.2)
[✓] VS Code (version 1.86.2)
[!] Proxy Configuration
    ! NO_PROXY is not set
[✓] Connected device (4 available)
[✓] Network resources

Activity

  1. dam-ease commented on Feb 21, 2024

    @dam-ease

    Helllo @fyxtc.
    Thanks for filing this!
    I can reproduce this on the latest master and stable channels.
    This occurs when a specific cacheExtent is set, setting cacheExtent: double.maxFinite works fine.

    stable, master flutter doctor -v

    [!] Flutter (Channel stable, 3.19.0, on macOS 14.2.1 23C71 darwin-arm64, locale
        en-NG)
        • Flutter version 3.19.0 on channel stable at
          /Users/damilolaalimi/sdks/flutter
        ! Warning: `dart` on your path resolves to
          /opt/homebrew/Cellar/dart/3.1.5/libexec/bin/dart, which is not inside your
          current Flutter SDK checkout at /Users/damilolaalimi/sdks/flutter.
          Consider adding /Users/damilolaalimi/sdks/flutter/bin to the front of your
          path.
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision bae5e49bc2 (7 days ago), 2024-02-13 17:46:18 -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.
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
        • Android SDK at /Users/damilolaalimi/Library/Android/sdk
        • Platform android-34, build-tools 34.0.0
        • ANDROID_HOME = /Users/damilolaalimi/Library/Android/sdk
        • Java binary at: /Applications/Android
          Studio.app/Contents/jbr/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build
          17.0.6+0-17.0.6b802.4-9586694)
        • 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 2022.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
          17.0.6+0-17.0.6b802.4-9586694)
    
    [!] Android Studio (version unknown)
        • Android Studio at /Users/damilolaalimi/Downloads/Android Studio
          Preview.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
        ✗ Unable to determine Android Studio version.
        • Java version OpenJDK Runtime Environment (build
          17.0.7+0-17.0.7b1000.6-10550314)
    
    [✓] VS Code (version 1.86.1)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.78.0
    
    [✓] VS Code (version 1.83.1)
        • VS Code at /Users/damilolaalimi/Downloads/Visual Studio Code.app/Contents
        • Flutter extension version 3.78.0
    
    [✓] Connected device (5 available)
        • TECNO CC7 (mobile)         • 05199339BL004045                     •
          android-arm64  • Android 9 (API 28)
        • Damilola’s iPhone (mobile) • 00008110-001964480AE1801E            • ios
          • iOS 17.1.1 21B91
        • iPhone 15 (mobile)         • DBCC5388-DA57-470E-8F0E-477EA0AA3C3A • ios
          • com.apple.CoreSimulator.SimRuntime.iOS-17-2 (simulator)
        • macOS (desktop)            • macos                                •
          darwin-arm64   • macOS 14.2.1 23C71 darwin-arm64
        • Chrome (web)               • chrome                               •
          web-javascript • Google Chrome 121.0.6167.184
    
    [✓] Network resources
        • All expected network resources are available.
    
    ! Doctor found issues in 2 categories.
    
    [!] Flutter (Channel master, 3.20.0-10.0.pre.82, on macOS 14.2.1 23C71 darwin-arm64, locale en-NG)
        • Flutter version 3.20.0-10.0.pre.82 on channel master at /Users/damilolaalimi/fvm/versions/master
        ! Warning: `dart` on your path resolves to /opt/homebrew/Cellar/dart/3.1.5/libexec/bin/dart, which is not inside your current Flutter SDK checkout at /Users/damilolaalimi/fvm/versions/master. Consider adding /Users/damilolaalimi/fvm/versions/master/bin to the front of your path.
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision e9f3244eaa (3 hours ago), 2024-02-21 00:27:23 -0500
        • Engine revision efc69946cb
        • Dart version 3.4.0 (build 3.4.0-157.0.dev)
        • DevTools version 2.33.0-dev.6
        • 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 34.0.0)
        • Android SDK at /Users/damilolaalimi/Library/Android/sdk
        • Platform android-34, build-tools 34.0.0
        • ANDROID_HOME = /Users/damilolaalimi/Library/Android/sdk
        • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)
        • 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 2022.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 17.0.6+0-17.0.6b802.4-9586694)
    
    [!] Android Studio (version unknown)
        • Android Studio at /Users/damilolaalimi/Downloads/Android Studio Preview.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
        ✗ Unable to determine Android Studio version.
        • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)
    
    [✓] VS Code (version 1.86.1)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.78.0
    
    [✓] VS Code (version 1.83.1)
        • VS Code at /Users/damilolaalimi/Downloads/Visual Studio Code.app/Contents
        • Flutter extension version 3.78.0
    
    [✓] Connected device (6 available)
        • TECNO CC7 (mobile)              • 05199339BL004045                     • android-arm64  • Android 9 (API 28)
        • Damilola’s iPhone (mobile)      • 00008110-001964480AE1801E            • ios            • iOS 17.1.1 21B91
        • iPhone 15 (mobile)              • DBCC5388-DA57-470E-8F0E-477EA0AA3C3A • ios            • com.apple.CoreSimulator.SimRuntime.iOS-17-2 (simulator)
        • macOS (desktop)                 • macos                                • darwin-arm64   • macOS 14.2.1 23C71 darwin-arm64
        • Mac Designed for iPad (desktop) • mac-designed-for-ipad                • darwin         • macOS 14.2.1 23C71 darwin-arm64
        • Chrome (web)                    • chrome                               • web-javascript • Google Chrome 121.0.6167.184
    
    [✓] Network resources
        • All expected network resources are available.
    
    ! Doctor found issues in 2 categories.
    

  2. added
    frameworkflutter/packages/flutter repository. See also f: labels.
    f: scrollingViewports, list views, slivers, etc.
    a: imagesLoading, displaying, rendering images
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    and removed
    in triagePresently being triaged by the triage team
    on Feb 21, 2024
  3. fyxtc commented on Feb 21, 2024

    @fyxtc
    Author

    @dam-ease
    Thanks for your reply.

    I commented out cacheExtent and still the same thing happens, it should have nothing to do with cacheExtent. As I said above, setting cacheExtent: double.maxFinite solves the problem, but directly causes a memory issue that crashes the app.

    And I don't think cacheExtent: double.maxFinite makes sense for ListView.builder, the ListView is supposed to specify the rendering area for performance purposes, so if you use that value, it's the same performance as using the ListView()/SingleChildScrollView().

  4. Piinks commented on Feb 27, 2024

    @Piinks
    Contributor

    I would recommend loading and caching the images only once. Here, it looks like the image is being requested every time the child is being built. So when the child is in view, it requests the image, and then when it scrolls out of view it is disposed. Then when it scrolls back, the image is requested again. And so on.
    Have you tried creating the Image.networks outside of ListView.builder? So they are only created/requested once?

  5. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Feb 27, 2024
  6. fyxtc commented on Feb 28, 2024

    @fyxtc
    Author

    @Piinks Thanks for the reply, I understand the reason, I personally think it's a bug, first of all I've used cacheExtent:1000, shouldn't the ListView cache 1000 pixels above the screen, why does scrolling back less than 100 pixels release all the images cached above it, which leads to a sudden decrease in the computed offset value, which directly causes the Scrolling back to the beginning and then loading from the beginning. It's so weird!

    I have used precache and CachedNetworkImage both with the same problem, I really don't know what I can do, please help me!

  7. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Feb 28, 2024
  8. fyxtc commented on Feb 28, 2024

    @fyxtc
    Author

    @Piinks Would it be possible to provide an option for cacheExtentUp so that I can set infinity

  9. Piinks commented on Feb 28, 2024

    @Piinks
    Contributor

    I found #99158, which looks like a duplicate of this issue, so I am going to close this to consolidate there.

    @fyxtc there are some discussions on different approaches to achieving your desired result there. Thank you for reporting, and please continue to follow at the linked issue. :)

  10. added
    r: duplicateIssue is closed as a duplicate of an existing issue
    on Feb 29, 2024
  11. github-actions commented on Mar 14, 2024

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

  12. locked as resolved and limited conversation to collaborators on Mar 14, 2024
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

    a: imagesLoading, displaying, rendering imagesf: scrollingViewports, list views, slivers, etc.found in release: 3.19Found to occur in 3.19found in release: 3.20Found to occur in 3.20frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: duplicateIssue is closed as a duplicate of an existing issueteam-frameworkOwned by Framework team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions