我已将资源文件添加到项目根目录中,其中包含图像、字体和声音。
\n\n在 pubspec.yml 文件中添加资产,如下所示:
\nflutter:\n\n uses-material-design: true\n\n assets:\n - assets/images/\nRun Code Online (Sandbox Code Playgroud)\n然后我尝试通过 Image.asset 添加图像,如下代码:
\nScaffold(\n appBar: AppBar(\n leading: Align(\n alignment: Alignment.centerRight,\n child: Text(\n "Uber",\n style: TextStyle(\n fontSize: 22.0,\n fontWeight: FontWeight.w400\n ),\n )\n ),\n ),\n body: Column(\n children: [\n Text(\n "riyad", \n style: TextStyle(color: Colors.black),\n ),\n Image.asset("car_android.png") <-- this like \n ],\n ),\n );\nRun Code Online (Sandbox Code Playgroud)\n但它显示了无法加载资产的异常:
\n\n\n解析图像编解码器时抛出以下断言:\n无法加载资源:car_android.png”
\n
我的回溯异常:
\n\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90 Exception caught by image resource service \xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\nThe following assertion was thrown resolving an image codec:\nUnable to load asset: car_android.png\n\nWhen the exception was thrown, this was the stack\n#0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:224:7)\n<asynchronous suspension>\n#1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:672:14)\n<asynchronous suspension>\nImage provider: AssetImage(bundle: null, name: "car_android.png")\nImage key: AssetBundleImageKey(bundle: PlatformAssetBundle#243b1(), name: "car_android.png", scale: 1.0)\nRun Code Online (Sandbox Code Playgroud)\n我尝试运行命令 flutter clean 命令来清理缓存,然后再次运行应用程序,但同样的问题再次出现,它无法加载资源。 \n我是 flutter 的新手,请提供描述性答案,并建议我一些博客读
\n指定图片路径如下
Image.asset("assets/images/car_android.png")
Run Code Online (Sandbox Code Playgroud)
或者
Image(image: AssetImage("assets/images/car_android.png"))
Run Code Online (Sandbox Code Playgroud)
问题是 flutter 无法识别图像的确切存储位置。
如果问题仍然存在,请尝试进行热重启,这将解决您的问题。
如果您更新 pubspec.yaml 文件,然后运行flutter pub get以更新库(如果您的 IDE 没有自动更新)。
| 归档时间: |
|
| 查看次数: |
10403 次 |
| 最近记录: |