我将图像添加到图像文件夹中,并在 flutter 中启用“yaml”文件中的资源。当我给出资产图像的路径并调试代码时,它显示“无法加载资产”的异常。未找到资产”。在 Pubspec.yaml 文件中,启用资产根本不会出现错误。 这是代码和输出窗口的图片。
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: gradientcolors, // reusable gradient colors argument
begin: startAlignment,
end: endAlignment,
),
),
child: Center(
child: Image.asset('images/blank-profile-picture-973460_640.png'),
),
);
}
我尝试在 lib 目录中以及单独创建图像目录,但它不适用于两种类型..
- images/blank-profile-picture-973460_640.png```
我对代码做了一些更改,现在它也没有显示图像.. 图像变化如下..
class _RollingDiceState extends State<RollingDice> {
var activeDice = 'images/dice-1.png';
void rollDice() {
var diceroll = Random().nextInt(6) + 1;
setState(() {
activeDice = 'images/dice-$diceroll.png';
});
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
Image.asset(
activeDice, // Passing the value of variable out of function
scale: 0.5,
),
const SizedBox(
height: 20,
),
TextButton(
onPressed:
rollDice, // Passing pointer as value of a function instead of function call
style: TextButton.styleFrom(
foregroundColor: Colors.white,
textStyle: const TextStyle(fontSize: 20)),
child: const Text("Roll Dice"),
),
],
);
}
}
[根目录为][1] [1]:https://i.stack.imgur.com/JffNo.png