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()); + }); }