我正在Flutter中创建一个Hangman应用程序,我希望当寿命缩短时,主图像可以更新为另一个。问题是,当我第一次运行该应用程序时,出现以下错误:
I/flutter (21336): ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
I/flutter (21336): The following assertion was thrown resolving an image codec:
I/flutter (21336): Unable to load asset: assets/Hangman 10.png
I/flutter (21336):
I/flutter (21336): When the exception was thrown, this was the stack:
I/flutter (21336): #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)
I/flutter (21336): <asynchronous suspension>
I/flutter (21336): #1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:484:44)
I/flutter (21336): #2 AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:469:14)
I/flutter (21336): #3 ImageProvider.resolve.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:327:17)
I/flutter (21336): #4 ImageCache.putIfAbsent (package:flutter/src/painting/image_cache.dart:160:22)
I/flutter (21336): #5 ImageProvider.resolve.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:325:84)
I/flutter (21336): (elided 13 frames from package dart:async)
I/flutter (21336):
I/flutter (21336): Image provider: AssetImage(bundle: null, name: "assets/Hangman 10.png")
I/flutter (21336): Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#133ec(), name: "assets/Hangman 10.png",
I/flutter (21336): scale: 1.0)
I/flutter (21336): ════════════════════════════════════════════════════════════════════════════════════════════════════
然后,如果我只是热重新加载,则将加载图像,并且该应用程序可以正常运行。 我扑扑的医生说一切都很好:
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, v1.12.13+hotfix.9, on Microsoft Windows [Version 10.0.18363.778], locale en-US)
[√] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
[√] Android Studio (version 3.6)
[√] VS Code (version 1.44.2)
[√] Connected device (1 available)
• No issues found!
这是我的文件夹的结构 asset image name and location
还有我的main.dart文件
import 'package:flutter/material.dart';
import 'nouns.dart';
import 'keyboard_ws.dart';
void main() => runApp(MaterialApp(home: Home()));
String word = 'wordToBeGuessed'
int lives = 10;
List<String> lettersEntered = [];
class Home extends StatefulWidget {
@override
HomeState createState() => HomeState();
}
String word = 'savannah';
int lives = 10;
List<String> lettersEntered = [];
List<String> wordShowList = List.filled(word.length, '_');
String displayWord = wordShowList.join(' ');
class HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Hangman'),
centerTitle: true,
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
child: Container(
padding: EdgeInsets.symmetric(vertical: 30, horizontal: 20),
child: Image(
image: AssetImage('assets/Hangman $lives.png'),
gaplessPlayback: true,
),
)),
Container(
child: Text(
displayWord,
style: TextStyle(fontSize: 30),
),
),
Keyboard(lettersEntered, sendKey)
],
),
);
}
}
我的pubspec.yaml文件中引用了我的资产文件夹,如下所示:
flutter:
assets:
- assets/
答案 0 :(得分:1)
原因:您的图片名称带有空格
解决方案:Unable to load asset" when the asset path contains a space
https://github.com/flutter/flutter/issues/47705#issuecomment-621728294
已在当前Beta v1.17.0-3.2.pre
和dev v1.18.0-6.0.pre
中修复(但在稳定的v1.12.13+hotfix.9
中未修复)
您可以考虑切换到新版本
工作演示
完整代码
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: Container(
padding: EdgeInsets.symmetric(vertical: 30, horizontal: 20),
child: Image(
image: AssetImage('assets/images/alarm space.png'),
gaplessPlayback: true,
),
)),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}