Baseflow / Baseflow/flutter_cached_network_image

Decoded image has been disposed

Open
#805 7 comments 5 reactions 0 assignees View on GitHub
Dominant language
Dart
Stars
2.6k
Forks
731
Avg merge
3d 9h
Merged PRs (30d)
1

Description

## 🐛 Bug Report

### Expected behavior
Should load the image correctly.

### Reproduction steps
I'm checking how much memory is currently in the cache and then cleaning the cache when full.

I'm receiving the following logs on Crashlytics:

```
Non-fatal Exception: io.flutter.plugins.firebase.crashlytics.FlutterError: Exception: Decoded image has been disposed. Error thrown null.
at FirebaseCrashlytics.recordError(firebase_crashlytics.dart:119)
at CustomExtendedImage.build.(custom_extended_image.dart:72)
at CachedNetworkImage._octoErrorBuilder(cached_image_widget.dart:318)
```

Check the code:

```dart
void _checkMemory() {
late int maxBytes;
final ConfigProvider configProvider = locator();
if (Platform.isAndroid) {
maxBytes = 1024 * 1024 * 100 * configProvider.androidMaxCache; // 500 MB
} else {
maxBytes = 1024 * 1024 * 100 * configProvider.iOSMaxCache; // 400 MB
}
FirebaseCrashlytics.instance
.log('Max bytes in cache for images: ${maxBytes.toString()}');

ImageCache _imageCache = PaintingBinding.instance.imageCache;
if (_imageCache.currentSize >= 400 ||
_imageCache.currentSizeBytes >= maxBytes) {
FirebaseCrashlytics.instance.log(
'Current image cache size: ${_imageCache.currentSizeBytes}. Its greater than the max: ${maxBytes.toString()} or greater than 400mb.\nCleaning images from cache.');

_imageCache.clearLiveImages();
_imageCache.clear();

FirebaseCrashlytics.instance.log('Images removed from cache.');
return;
}
}

@override
Widget build(BuildContext context) {
final int? cacheWidthInt =
cacheWidth ? MediaQuery.of(context).size.width.toInt() : null;
final int? cacheHeightInt =
cacheHeight ? MediaQuery.of(context).size.height.toInt() : null;
_checkMemory();
return CachedNetworkImage(
imageUrl: url,
fit: BoxFit.cover,
height: height ?? MediaQuery.of(context).size.height,
width: width ?? MediaQuery.of(context).size.width,
placeholderFadeInDuration: Duration.zero,
fadeInDuration: Duration.zero,
fadeOutDuration: Duration.zero,
errorWidget: (context, url, error) {
FirebaseCrashlytics.instance.log(
'An error occur loading image: $url\nShowing error placeholder.',
);

FirebaseCrashlytics.instance.recordError(error, null);

return const Icon(Icons.error);
},
memCacheWidth: cacheWidth ? (cacheWidthInt! * 1.5).toInt() : null,
color: color,
filterQuality: FilterQuality.medium,
memCacheHeight: cacheHeight ? (cacheHeightInt! * 1.4).toInt() : null,
);
}
```

### Configuration

**Version:** 3.2.3

**Platform:**
- [x] :robot: Android

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.