Skip to content

StateError: Bad state: Cannot clone a disposed image. The clone() method of a previously-disposed Image was called. Once an Image object has been disposed, it can no longer be used to create handles, as the underlying data may have been released. #74

@iBelow

Description

@iBelow

After adding flutter_blurhash to my project, I started seeing the following runtime error:

StateError: Bad state: Cannot clone a disposed image.
The clone() method of a previously-disposed Image was called. Once an Image object has been disposed, it can no longer be used to create handles, as the underlying data may have been released.

When using BlurHash with a network image, Flutter sometimes throws StateError: Bad state: Cannot clone a disposed image.

code example:

/// {@template image_loader}
/// ImageLoader widget.
/// {@endtemplate}
class ImageLoader extends StatefulWidget {
  /// {@macro image_loader}
  const ImageLoader({
    required this.imgUrl,
    this.blurHash,
    this.isCupertinoStyle = true,
    this.fit = BoxFit.cover,
    this.onErrorWidget,
    this.borderRadius,
    this.backgroundColor,
    this.height,
    this.width,
    this.blendMode,
    this.color,
    super.key,
  });

  final String? imgUrl;
  final String? blurHash;
  final bool isCupertinoStyle;
  final BoxFit? fit;
  final Widget? onErrorWidget;
  final BorderRadius? borderRadius;
  final Color? backgroundColor;
  final double? height;
  final double? width;
  final BlendMode? blendMode;
  final Color? color;

  @override
  State<ImageLoader> createState() => _ImageLoaderState();
}

class _ImageLoaderState extends State<ImageLoader> {
  final int animationDuration = 800;

  @override
  Widget build(BuildContext context) => widget.imgUrl != null && widget.imgUrl!.isNotEmpty
      ? ColoredBox(
          color: widget.backgroundColor ?? Colors.transparent,
          child: ClipRRect(
            borderRadius: widget.borderRadius ?? BorderRadius.circular(0),
            child: widget.blurHash != null &&
                    widget.blurHash!.isNotEmpty &&
                    !widget.imgUrl!.contains('.png')
                ? BlurHash(
                    hash: widget.blurHash!,
                    decodingHeight: widget.height?.round() ?? 32,
                    decodingWidth: widget.width?.round() ?? 32,
                    color: Colors.white,
                    image: widget.imgUrl,
                    imageFit: widget.fit ?? BoxFit.cover,
                    curve: Curves.easeInOut,
                    duration: Duration(milliseconds: animationDuration),
                  )
                : Image.network(
                    widget.imgUrl!,
                    fit: widget.fit,
                    height: widget.height,
                    width: widget.width,
                    color: widget.color,
                    colorBlendMode: widget.blendMode,
                    cacheHeight: widget.height?.round(),
                    cacheWidth: widget.width?.round(),
                    errorBuilder: (context, error, stackTrace) => widget.onErrorWidget ??
                        const Icon(Icons.hide_image_rounded),
                  ),
          ),
        )
      : Image.asset('assets/placeholder.png', fit: widget.fit);
}

This error does not appear when using Image.network directly.
It only happens when BlurHash is used as a placeholder together with a network image.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions