FadeInImage.assetNetwork 需要绝对路径。占位符的相对路径不起作用。(颤抖中)

阿尤什·沙阿

我正在显示FadeInImage.assetNetwork需要两个成员的图像imageplaceholder

    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'

并确保您在 pubspec.yaml 中的资产中添加了图像路径,如下所示

assets:
- assets/images/

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章