From cbbf46f191e9a16ef706ede07bd64ad84d84dee9 Mon Sep 17 00:00:00 2001 From: Igor Khramtsov Date: Wed, 20 Mar 2024 22:48:17 +0400 Subject: [PATCH] fix(cache): clear _pending in case of exception --- packages/flutter_svg/lib/src/cache.dart | 2 ++ packages/flutter_svg/test/cache_test.dart | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/packages/flutter_svg/lib/src/cache.dart b/packages/flutter_svg/lib/src/cache.dart index adfd9bb2..46a64133 100644 --- a/packages/flutter_svg/lib/src/cache.dart +++ b/packages/flutter_svg/lib/src/cache.dart @@ -85,6 +85,8 @@ class Cache { _pending.remove(key); _add(key, data); result = data; // in case it was a synchronous future. + }).catchError((Object e, _) { + _pending.remove(key); }); } if (result != null) { diff --git a/packages/flutter_svg/test/cache_test.dart b/packages/flutter_svg/test/cache_test.dart index 1a049107..c72c2d46 100644 --- a/packages/flutter_svg/test/cache_test.dart +++ b/packages/flutter_svg/test/cache_test.dart @@ -133,4 +133,19 @@ void main() { await null; expect(cache.count, 2); }); + + test('Exception thrown during loader execution is handled', () async { + final Cache cache = Cache(); + + Future futureThatThrows() async => + Future.error(Exception('Exception inside future')); + await expectLater( + cache.putIfAbsent(1, () => futureThatThrows()), + throwsA(anything), + ); + + Future futureThatDoesntThrow() async => ByteData(1); + // Doesn't throw an error + await cache.putIfAbsent(1, () => futureThatDoesntThrow()); + }); }