【发布时间】:2021-09-06 15:30:12
【问题描述】:
我正在通过FadeInImage.assetNetwork 显示图像,这需要两个成员:image 和placeholder。
ClipRRect( //forcing image to rounded corners
borderRadius: BorderRadius.only(
topLeft: Radius.circular(15),
topRight: Radius.circular(15),
),
child: FadeInImage.assetNetwork(
image: imageUrl,
placeholder: '/Users/aayush/Documents/Flutter/Apps/meals_app/assets/images/food-placeholder.png', //here full path is necessary. relative path is not working.
// placeholder: '../../assets/images/food-placeholder.png', //this relative path is not working.
height: 250,
width: double.infinity,
fit: BoxFit.cover, //will resize and crop the image
),
),
这是我的文件夹结构:
目前我在lib/widgets/meal_item.dart,占位符图像位于assets/images/food-placeholder.png。
所以,我的相对路径将是:../../assets/images/food-placeholder.png。
当我在这里尝试使用相对路径时的错误消息:
════════ Exception caught by image resource service ════════════════════════════
The following assertion was thrown resolving an image codec:
Unable to load asset: ../../assets/images/food-placeholder.png
When the exception was thrown, this was the stack
#0 PlatformAssetBundle.load
package:flutter/…/services/asset_bundle.dart:224
<asynchronous suspension>
#1 AssetBundleImageProvider._loadAsync
package:flutter/…/painting/image_provider.dart:672
<asynchronous suspension>
Image provider: AssetImage(bundle: null, name: "../../assets/images/food-placeholder.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#df521(), name: "../../assets/images/food-placeholder.png", scale: 1.0)
我尝试了很多次,但它不仅仅在这个小部件中工作。相对路径在其他小部件中运行良好。 请为此提供解决方案或建议,以便我可以显示占位符图像,直到获取 url 图像。
【问题讨论】:
-
其实问题出在相对路径上。相对路径应为:
assets/images/food-placeholder.png。谢谢大家的回答:)
标签: flutter dart relative-path placeholder absolute-path