在我的颤振应用程序中,我想根据条件加载资产:我的代码是作为导入包运行还是作为我目前正在开发的应用程序运行。
问题是这个 try/catch 块总是在 try 部分失败:
AssetImage _getBackgroundImage() {
try {
return AssetImage(
'lib/assets/images/menu_background.png',
package: 'my_package',
);
} catch (e) {
return AssetImage('lib/assets/images/menu_background.png');
}
}
运行输出:
======== Exception caught by image resource service ================================================
The following assertion was thrown resolving an image codec:
Unable to load asset: packages/my_package/lib/assets/images/menu_background.png
When the exception was thrown, this was the stack:
#0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:227:7)
<asynchronous suspension>
#1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:667:14)
<asynchronous suspension>
Image provider: AssetImage(bundle: null, name: "packages/my_package/lib/assets/images/menu_background.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#e01ff(), name: "packages/my_package/lib/assets/images/menu_background.png", scale: 1.0)
====================================================================================================