From 407f276ce90a8c5988b8e52be3188839204a2ee3 Mon Sep 17 00:00:00 2001 From: nilsreichardt <24459435+nilsreichardt@users.noreply.github.com> Date: Thu, 3 Dec 2026 14:56:18 +0100 Subject: [PATCH] fix: clear a running resize when its listener cancels getImageFile removed the shared resize stream only after yield* had completed. A listener that cancelled, for example on a download error, skipped that line, so the next call for the same url and size got the finished stream and ended without a file or an error. Fixes #439 Co-Authored-By: Claude --- flutter_cache_manager/CHANGELOG.md | 4 +++ .../cache_managers/image_cache_manager.dart | 9 +++-- flutter_cache_manager/pubspec.yaml | 2 +- .../test/image_cache_manager_test.dart | 33 +++++++++++++++++++ 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/flutter_cache_manager/CHANGELOG.md b/flutter_cache_manager/CHANGELOG.md index f1d60241..a4d1fe6c 100644 --- a/flutter_cache_manager/CHANGELOG.md +++ b/flutter_cache_manager/CHANGELOG.md @@ -1,3 +1,7 @@ +## [3.5.1] - 2026-12-03 + +* Fixes the next `getImageFile` call with `maxWidth` or `maxHeight` ending without a file or an error after a listener stopped listening early, for example on a download error ([#439](https://github.com/Baseflow/flutter_cache_manager/issues/439)). + ## [3.5.0] - 2026-11-04 * **DEPRECATION** Deprecates `supportedFileNames`: whether an image is resized is now decided by the image engine from the file's content instead of from the file extension derived from the `Content-Type` header of the response ([#475](https://github.com/Baseflow/flutter_cache_manager/pull/475)) diff --git a/flutter_cache_manager/lib/src/cache_managers/image_cache_manager.dart b/flutter_cache_manager/lib/src/cache_managers/image_cache_manager.dart index 858ad146..8760ebf6 100644 --- a/flutter_cache_manager/lib/src/cache_managers/image_cache_manager.dart +++ b/flutter_cache_manager/lib/src/cache_managers/image_cache_manager.dart @@ -73,8 +73,13 @@ mixin ImageCacheManager on BaseCacheManager { ).asBroadcastStream(); _runningResizes[resizedKey] = runningResize; } - yield* runningResize; - _runningResizes.remove(resizedKey); + try { + yield* runningResize; + } finally { + // Also when the listener cancels, for example on a download error. + // Otherwise the next call would get the finished stream and no file. + _runningResizes.remove(resizedKey); + } } final Map> _runningResizes = {}; diff --git a/flutter_cache_manager/pubspec.yaml b/flutter_cache_manager/pubspec.yaml index 49ca35b2..9053923b 100644 --- a/flutter_cache_manager/pubspec.yaml +++ b/flutter_cache_manager/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_cache_manager description: Generic cache manager for flutter. Saves web files on the storages of the device and saves the cache info using sqflite. -version: 3.5.0 +version: 3.5.1 homepage: https://github.com/Baseflow/flutter_cache_manager topics: - cache diff --git a/flutter_cache_manager/test/image_cache_manager_test.dart b/flutter_cache_manager/test/image_cache_manager_test.dart index 0e1593c0..8de26dd4 100644 --- a/flutter_cache_manager/test/image_cache_manager_test.dart +++ b/flutter_cache_manager/test/image_cache_manager_test.dart @@ -5,9 +5,11 @@ import 'dart:ui'; import 'package:flutter/material.dart'; import 'package:flutter_cache_manager/flutter_cache_manager.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:mockito/mockito.dart'; import 'cache_manager_test.dart'; import 'helpers/config_extensions.dart'; +import 'helpers/mock_file_service.dart'; import 'helpers/test_configuration.dart'; import 'test_images.dart'; @@ -127,6 +129,37 @@ void main() { config.verifyDownloadCall(); expect(progress, isNotEmpty); }); + + test('Resized image should download again after an error', () async { + var config = createTestConfig(); + var error = HttpExceptionWithStatus( + 500, + 'Invalid statusCode: 500', + uri: Uri.parse(fileUrl), + ); + var calls = 0; + when( + config.mockFileService.get(any, headers: anyNamed('headers')), + ).thenAnswer((_) async { + calls++; + if (calls == 1) throw error; + return TestResponse(); + }); + var cacheManager = TestCacheManager(config); + + // Stops listening at the error, like CachedNetworkImage does. + await expectLater( + cacheManager.getImageFile(fileUrl, maxWidth: 100).first, + throwsA(error), + ); + var result = + await cacheManager.getImageFile(fileUrl, maxWidth: 100).last + as FileInfo; + + var image = await result.file.readAsBytes(); + await verifySize(image, 100, 100); + config.verifyDownloadCall(2); + }); }); group('Test image detection', () {