Skip to content

ColorScheme will removeListener on imageStream twice if there is error loading the image. #170413

@zjhaoz

Description

@zjhaoz

Steps to reproduce

Create a widget which use the colorScheme to extract seed color from a imageProvider, and use the created colorScheme to set the color of the widget, intentionally use an non-exist network image url.

Flutter will first throw an exception saying that failed to render the image, wait for 5 seconds which is the threshold for timeout, flutter will throw another Exception saying that
"An ImageStream is considered disposed once at least one listener has been added and subsequently all listeners have been removed and no handles are outstanding from the keepAlive method"

Expected results

Should not removeListener twice on the imageStream, only "Failed to render the image" should be thrown, the second exception should not be thrown.

Actual results

Flutter throws two exceptions, first exception is
"Failed to render image:"
And second exception is:
"An ImageStream is considered disposed once at least one listener has been added and subsequently all listeners have been removed and no handles are outstanding from the keepAlive method"

Code sample

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(useMaterial3: false),
      title: 'Flutter Demo',
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Color? _textColors;

  @override
  void initState() {
    super.initState();
    _init();
  }

  void _init() async {
    final dynamicColorScheme = await ColorScheme.fromImageProvider(
      provider: NetworkImage(
        'random_non_exist_image.png',
      ),
    );
    _textColors = dynamicColorScheme.primary;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title, style: TextStyle(fontFamily: 'ProductSans')),
      ),
      body: Center(
        child: Text(
          style: TextStyle(color: _textColors ?? Colors.black),
          'Dynamic color text',
          key: Key('DynamicColorText'),
        ),
      ),
    );
  }
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
The following _Exception was thrown when reporting an error to an image listener:
Exception: Failed to render image: Invalid argument(s): No host specified in URI
development/bug/random_non_exist_image.png

When the exception was thrown, this was the stack:
#0      ColorScheme._imageProviderToScaled.<anonymous closure> (package:flutter/src/material/color_scheme.dart:2148:9)
#1      ImageStreamCompleter.reportError (package:flutter/src/painting/image_stream.dart:816:22)
#2      new MultiFrameImageStreamCompleter.<anonymous closure> (package:flutter/src/painting/image_stream.dart:986:9)
(elided 8 frames from dart:async)
════════════════════════════════════════════════════════════════════════════════════════════════════

Another exception was thrown: Invalid argument(s): No host specified in URI
random_non_exist_image.png
The Flutter DevTools debugger and profiler on Linux is available at: http://127.0.0.1:9100?uri=http://127.0.0.1:42197/k4fBry2gAso=/
[ERROR:flutter/runtime/dart_vm_initializer.cc(40)] Unhandled Exception: Bad state: Stream has been disposed.
An ImageStream is considered disposed once at least one listener has been added and subsequently all listeners have been removed and no handles are outstanding from the keepAlive method.
To resolve this error, maintain at least one listener on the stream, or create an ImageStreamCompleterHandle from the keepAlive method, or create a new stream for the image.
#0      ImageStreamCompleter._checkDisposed (package:flutter/src/painting/image_stream.dart:698:7)
#1      ImageStreamCompleter.removeListener (package:flutter/src/painting/image_stream.dart:646:5)
#2      MultiFrameImageStreamCompleter.removeListener (package:flutter/src/painting/image_stream.dart:1145:11)
#3      ImageStream.removeListener (package:flutter/src/painting/image_stream.dart:393:26)
#4      ColorScheme._imageProviderToScaled.<anonymous closure> (package:flutter/src/material/color_scheme.dart:2153:14)
#5      Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
#6      _Timer._runTimers (dart:isolate-patch/timer_impl.dart:410:19)
#7      _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:441:5)
#8      _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:193:12)

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.32.2, on Debian GNU/Linux rodete 6.12.20-1rodete1-amd64, locale en_US.UTF-8) [227ms]
    • Flutter version 3.32.2 on channel stable at /usr/local/google/home/haozhu/development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 8defaa71a7 (7 days ago), 2025-06-04 11:02:51 -0700
    • Engine revision 1091508939
    • Dart version 3.8.1
    • DevTools version 2.45.1

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.1) [1,849ms]
    • Android SDK at /usr/local/google/home/haozhu/Android/Sdk
    • Platform android-35, build-tools 35.0.1
    • Java binary at: /opt/android-studio-with-blaze-stable/jbr/bin/java
      This is the JDK bundled with the latest Android Studio installation on this machine.
      To manually set the JDK path, use: `flutter config --jdk-dir="path/to/jdk"`.
    • Java version OpenJDK Runtime Environment (build 21.0.6+-13368085-b895.109)
    • All Android licenses accepted.

[✓] Chrome - develop for the web [126ms]
    • Chrome at google-chrome

[✓] Linux toolchain - develop for Linux desktop [474ms]
    • Debian clang version 19.1.7 (3)
    • cmake version 3.31.6
    • ninja version 1.12.1
    • pkg-config version 1.8.1
    • OpenGL core renderer: llvmpipe (LLVM 19.1.7, 256 bits) (X11)
    • OpenGL core version: 4.5 (Core Profile) Mesa 25.0.2-1 (X11)
    • OpenGL core shading language version: 4.50 (X11)
    • OpenGL ES renderer: llvmpipe (LLVM 19.1.7, 256 bits) (X11)
    • OpenGL ES version: OpenGL ES 3.2 Mesa 25.0.2-1 (X11)
    • OpenGL ES shading language version: OpenGL ES GLSL ES 3.20 (X11)
    • GL_EXT_framebuffer_blit: yes (X11)
    • GL_EXT_texture_format_BGRA8888: yes (X11)

[✓] Android Studio (version 2024.3.2) [124ms]
    • Android Studio at /opt/android-studio-with-blaze-stable
    • 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 21.0.6+-13368085-b895.109)

[✓] Android Studio (version 2021.3) [122ms]
    • Android Studio at /opt/android-studio-with-blaze-2021.3
    • Flutter plugin version 71.2.3
    • Dart plugin version 213.7433
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)

[✓] IntelliJ IDEA Ultimate Edition (version 2022.3) [17ms]
    • IntelliJ at /opt/intellij-ue-2022.3
    • 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

[✓] IntelliJ IDEA Community Edition (version 2022.3) [16ms]
    • IntelliJ at /opt/intellij-ce-2022.3
    • 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

[✓] Connected device (2 available) [180ms]
    • Linux (desktop) • linux  • linux-x64      • Debian GNU/Linux rodete 6.12.20-1rodete1-amd64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 137.0.7151.103

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

• No issues found!

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work lista: error messageError messages from the Flutter frameworka: imagesLoading, displaying, rendering imagesf: material designflutter/packages/flutter/material repository.found in release: 3.32Found to occur in 3.32found in release: 3.33Found to occur in 3.33frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer versionteam-designOwned by Design Languages teamtriaged-designTriaged by Design Languages team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions