Flutter 错误:无法加载资源
这是我的应用程序的文件夹结构Flutter 错误:无法加载资源,flutter,assets,Flutter,Assets,这是我的应用程序的文件夹结构 .idea .vscode android build fonts Oxygen-Bold.tff Oxygen-Light.tff Oxygen-Regular.tff images pizza0.png pizza1.png ios lib ui home.dart main.dart test .gitignore .metadata .packages app_widgets.iml pubspec.lock pubspec.yaml RE
.idea
.vscode
android
build
fonts
Oxygen-Bold.tff
Oxygen-Light.tff
Oxygen-Regular.tff
images
pizza0.png
pizza1.png
ios
lib
ui
home.dart
main.dart
test
.gitignore
.metadata
.packages
app_widgets.iml
pubspec.lock
pubspec.yaml
README.md
在我的pubspec.yaml
文件中,我加载如下字体和资源
flutter:
uses-material-design: true
assets:
- images/pizza0.png
- images/pizza1.png
fonts:
- family: Oxygen
fonts:
- asset: fonts/Oxygen-Regular.ttf
- asset: fonts/Oxygen-Bold.ttf
weight: 700
- asset: fonts/Oxygen-Light.ttf
weight: 300
flutter:
uses-material-design: true
assets:
- images/xxx.png
flutter:
assets:
- assets/images/image.png
我没有收到此pubspec.yaml
的任何错误,并且运行flatterpackagesget
会给出0的退出代码
在我家,我有以下课程:
class PizzaImageWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
AssetImage pizzaAsset = AssetImage('images/pizza0.png');
Image image = Image(image: pizzaAsset, width: 400, height: 400);
return Container(
child: image,
);
}
}
我在其他地方使用它来显示图像(代码省略):
这座大楼没有错误。颤振医生-v没有给出任何错误,颤振分析-v也没有给出任何错误。.apk似乎构建得很好,但当应用程序在我的手机上打开时,我在asset_bundle.dart中发现以下错误:
发生异常。错误(无法加载资源:
图片/pizza0.png)
此类在asset_bundle.dart文件中引发错误:
/// An [AssetBundle] that loads resources using platform messages.
class PlatformAssetBundle extends CachingAssetBundle {
@override
Future<ByteData> load(String key) async {
final Uint8List encoded = utf8.encoder.convert(Uri(path: Uri.encodeFull(key)).path);
final ByteData asset =
await BinaryMessages.send('flutter/assets', encoded.buffer.asByteData());
if (asset == null)
throw FlutterError('Unable to load asset: $key');
return asset;
}
}
///使用平台消息加载资源的[AssetBundle]。
类PlatformAssetBundle扩展了CachingAssetBundle{
@凌驾
未来加载(字符串键)异步{
最终Uint8List encoded=utf8.encoder.convert(Uri(路径:Uri.encodeFull(key)).path);
最终ByteData资产=
wait BinaryMessages.send('flatter/assets',encoded.buffer.asByteData());
如果(资产==null)
抛出错误('无法加载资产:$key');
归还资产;
}
}
pizza0.png文件和pizza1.png文件都会发生这种情况。这些文件在树结构中可见,无论是在Windows资源管理器中还是在VS代码中。字体资源加载时不会出现问题
这是运行颤振运行-v时得到的输出:
[+1068毫秒]I/颤振(6489):══╡ 图像资源捕获到异常
服务╞════════════════════════════════════════════════════ [ +9
ms]I/flatter(6489):以下断言是在解决
图像编解码器:[+2毫秒]I/flutter(6489):无法加载资源:
图像/pizza0.png[+2毫秒]I/颤振(6489):[+1毫秒]I/颤振(
6489):抛出异常时,这是堆栈:[+2毫秒]
I/颤振(6489):#0平台组件。载荷
(包:颤振/src/服务/资产包。dart:221:7)[+1毫秒]
I/颤振(6489):[+1毫秒]I/颤振(
6489):#1个AssetBundleImageProvider._loadAsync
(软件包:颤振/src/painting/image_provider.dart:429:44)[+1毫秒]
I/颤振(6489):[+1毫秒]I/颤振(
6489):#2 AssetBundleImageProvider.load
(包装:颤振/src/painting/image_提供程序。dart:414:14)[+1毫秒]
I/flatter(6489):#3图像提供程序。解决。。
(软件包:颤振/src/painting/image_provider.dart:267:86)[+4毫秒]
I/flatter(6489):#4 ImageCache.putIfAbsent
(包装:颤振/src/painting/image_缓存。dart:143:20)[+3毫秒]
I/flatter(6489):#5 ImageProvider.resolve。
(软件包:颤振/src/painting/image_provider.dart:267:63)[+3毫秒]
I/颤振(6489):(从包dart中删除8帧:异步)[+1]
ms]I/flatter(6489):[+1 ms]I/flatter(6489):图像提供程序:
AssetImage(bundle:null,name:“images/pizza0.png”)[+3ms]
I/颤振(6489):图像键:AssetBundleImageKey(捆绑:
PlatformAssetBundle#20fc8(),名称:“images/pizza0.png”,[+1毫秒]
I/颤振(6489):比例:1.0)[+2毫秒]I/颤振(6489):
════════════════════════════════════════════════════════════════════════════════════════════════════
您应该考虑<代码>资产< <代码> < /p>的缩进。
flutter:
assets:
- images/pizza1.png
- images/pizza0.png
更多详情:
flutter:
[2 whitespaces or 1 tab]assets:
[4 whitespaces or 2 tabs]- images/pizza1.png
[4 whitespaces or 2 tabs]- images/pizza0.png
不要费力地为每个图像资源添加路径,只需指定图像目录的路径即可。
只要确保使用正确的缩进,因为pubspec.yaml是缩进敏感的
flutter:
uses-material-design: true
assets:
- images/
您只需访问每个图像即可
new Image.asset('images/pizza1.png',width:300,height:100)
我也有同样的问题。我刚刚运行了
“$flatterclean”
,然后一切都正常了
我纠正了同样的问题,您只需将这两个(使用材质设计:true和assets)放在同一列中,然后单击升级依赖项,然后重新启动android studio
我遇到了一个类似的问题,我在这里解决了:
uses-material-design: true
assets:
- assets/images/
之后,请执行以下操作:
Flutter Clean
我也有这个问题。我认为Flitter缓存图像的方式有一个缺陷。我的猜测是,当您第一次尝试加载
pizza0.png
时,它不可用,而flatter缓存了这个失败的结果。然后,即使在添加了正确的图像后,“颤振”仍然认为它不可用
这都是猜测工作,基于我有同样的问题的事实,在应用程序启动时调用此选项修复了我的问题:
imageCache.clear();
这将清除图像缓存,这意味着Flatter随后将尝试加载新图像,而不是搜索缓存
PS我还发现,无论何时更改任何现有图像,都需要调用此函数,原因相同-Flutter将加载旧的缓存版本。另一种方法是重命名图像。这个问题过去几乎让我发疯。为了支持其他人的说法,在确保yaml文件上的所有缩进都已纠正且问题仍然存在后,在Android studio的终端上运行“flutter clean”命令。与Flatter 1.9一样,这应该可以解决问题。确保文件名不包含特殊字符,例如,2019年12月25日: 无论是谁,如果他面临的问题是在Flutter中没有显示图像,那么让我给你一些简单的检查点:
第一点非常重要即使在,
flatter cleanimage: AssetImage(
'./lib/graphics/logo2.png'
),
width: 200,
height: 200,
);
static Future<String> resourceText(String resName) async
{
try
{
ZLibCodec zlc = new ZLibCodec(gzip:false,raw:true,level:9);
var data= await rootBundle.load('assets/path/to/$resName');
String result = new
String.fromCharCodes(zlc.decode(puzzleData.buffer.asUint8List()));
return puzzle;
} catch(e)
{
debugPrint('Resource Error $resName $e');
return '';
}
}
static Future<String> fallBackText(String textName) async
{
if (testCondtion) return 'Some Required Text';
else return resourceText('default');
}
flutter:
assets:
- assets
flutter:
assets:
- assets/sprites/
- assets/audio/
- assets/UI/
flutter:
uses-material-design: true
assets:
- images/
flutter:
uses-material-design: true
assets:
- assets/images/
flutter:
<space><space>assets:
<space><space><space><space>assets/
flutter:
assets:
assets/
flutter:
uses-material-design: true
assets:
- assets/my_icon.png
- assets/background.png
flutter:
uses-material-design: true
assets:
- directory/
- directory/subdirectory/
flutter:
uses-material-design: true
assets:
- images/xxx.png
assets:
- assets/sub_folder/
flutter:
assets:
- assets/images/image.png
AssetImage('images/heart.png', package: 'my_icons') // my_icons is your plugin name, in flutter plugin is also a package.
AssetImage('images\heart.png')
AssetImage('images/heart.png')
flutter:
uses-material-design: tru
assets:
- images/image1.jpg
Image.asset( 'assets\\images\\logo.png')