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 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
dart, flutter
领域
mobile-dev

调研方向

首先,使用 BlurHash 作为网络图像的占位符来重现报告的运行时错误,然后检查 package 的 BlurHash 图像处理路径和生命周期。将其与 Image.network 的直接行为进行比较。完成的标准是网络图像占位符不再尝试克隆已释放的图像,同时现有的图像加载和错误行为保持不变。

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
Dart
星标
576
派生
80
PR 合并指标
30 天内没有已合并 PR

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

fluttercommunity/flutter_blurhash 的其他 Issue

查看 fluttercommunity/flutter_blurhash 的全部 Issue

相似的 Issue

更多 Dart Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。