diff --git a/packages/flutter/lib/src/services/asset_bundle.dart b/packages/flutter/lib/src/services/asset_bundle.dart index 06d62f7003..5b9fadd9e6 100644 --- a/packages/flutter/lib/src/services/asset_bundle.dart +++ b/packages/flutter/lib/src/services/asset_bundle.dart @@ -81,9 +81,6 @@ abstract class AssetBundle { /// isolate to avoid jank on the main thread. Future loadString(String key, { bool cache = true }) async { final ByteData data = await load(key); - if (data == null) { - throw FlutterError('Unable to load asset: $key'); - } // 50 KB of data should take 2-3 ms to parse on a Moto G4, and about 400 μs // on a Pixel 4. if (data.lengthInBytes < 50 * 1024) { @@ -139,7 +136,7 @@ class NetworkAssetBundle extends AssetBundle { final HttpClientResponse response = await request.close(); if (response.statusCode != HttpStatus.ok) { throw FlutterError.fromParts([ - ErrorSummary('Unable to load asset: $key'), + _errorSummaryWithKey(key), IntProperty('HTTP status code', response.statusCode), ]); } @@ -255,7 +252,10 @@ class PlatformAssetBundle extends CachingAssetBundle { final ByteData? asset = await ServicesBinding.instance.defaultBinaryMessenger.send('flutter/assets', encoded.buffer.asByteData()); if (asset == null) { - throw FlutterError('Unable to load asset: $key'); + throw FlutterError.fromParts([ + _errorSummaryWithKey(key), + ErrorDescription('The asset does not exist or has empty data.'), + ]); } return asset; } @@ -284,8 +284,11 @@ class PlatformAssetBundle extends CachingAssetBundle { } try { return await ui.ImmutableBuffer.fromAsset(key); - } on Exception { - throw FlutterError('Unable to load asset: $key.'); + } on Exception catch (e) { + throw FlutterError.fromParts([ + _errorSummaryWithKey(key), + ErrorDescription(e.toString()), + ]); } } } @@ -294,6 +297,10 @@ AssetBundle _initRootBundle() { return PlatformAssetBundle(); } +ErrorSummary _errorSummaryWithKey(String key) { + return ErrorSummary('Unable to load asset: "$key".'); +} + /// The [AssetBundle] from which this application was loaded. /// /// The [rootBundle] contains the resources that were packaged with the diff --git a/packages/flutter/test/services/asset_bundle_test.dart b/packages/flutter/test/services/asset_bundle_test.dart index 89bb8cfe32..8a97df3daa 100644 --- a/packages/flutter/test/services/asset_bundle_test.dart +++ b/packages/flutter/test/services/asset_bundle_test.dart @@ -27,6 +27,8 @@ class TestAssetBundle extends CachingAssetBundle { } void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + test('Caching asset bundle test', () async { final TestAssetBundle bundle = TestAssetBundle(); @@ -72,8 +74,8 @@ void main() { expect( error.toStringDeep(), 'FlutterError\n' - ' Unable to load asset: key\n' - ' HTTP status code: 404\n', + ' Unable to load asset: "key".\n' + ' HTTP status code: 400\n', ); }, skip: isBrowser); // https://github.com/flutter/flutter/issues/39998 @@ -83,4 +85,20 @@ void main() { expect(bundle.toString(), 'NetworkAssetBundle#${shortHash(bundle)}($uri)'); }, skip: isBrowser); // https://github.com/flutter/flutter/issues/39998 + + test('Throws expected exceptions when loading not exists asset', () async { + late final FlutterError error; + try { + await rootBundle.load('not-exists'); + } on FlutterError catch (e) { + error = e; + } + expect( + error.message, + equals( + 'Unable to load asset: "not-exists".\n' + 'The asset does not exist or has empty data.', + ), + ); + }, skip: isBrowser); // https://github.com/flutter/flutter/issues/56314 } diff --git a/packages/flutter/test/widgets/image_test.dart b/packages/flutter/test/widgets/image_test.dart index 47af959a1e..a55d3e71b2 100644 --- a/packages/flutter/test/widgets/image_test.dart +++ b/packages/flutter/test/widgets/image_test.dart @@ -1997,7 +1997,13 @@ void main() { find.byKey(key), matchesGoldenFile('image_test.missing.1.png'), ); - expect(tester.takeException().toString(), startsWith('Unable to load asset: ')); + expect( + tester.takeException().toString(), + equals( + 'Unable to load asset: "missing-asset".\n' + 'The asset does not exist or has empty data.', + ), + ); await tester.pump(); await expectLater( find.byKey(key),