Flutter:无法加载资源文件“car.png”

Riy*_*dar 4 dart flutter

我已将资源文件添加到项目根目录中,其中包含图像、字体和声音。

\n

资产文件夹

\n

在 pubspec.yml 文件中添加资产,如下所示:

\n
flutter:\n\n  uses-material-design: true\n\n  assets:\n    - assets/images/\n
Run Code Online (Sandbox Code Playgroud)\n

然后我尝试通过 Image.asset 添加图像,如下代码:

\n
Scaffold(\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    );\n
Run Code Online (Sandbox Code Playgroud)\n

但它显示了无法加载资产的异常:

\n
\n

解析图像编解码器时抛出以下断言:\n无法加载资源:car_android.png”

\n
\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)\n
Run Code Online (Sandbox Code Playgroud)\n

我尝试运行命令 flutter clean 命令来清理缓存,然后再次运行应用程序,但同样的问题再次出现,它无法加载资源。 \n我是 flutter 的新手,请提供描述性答案,并建议我一些博客读

\n

Har*_*mar 5

指定图片路径如下

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 没有自动更新)。