首页 > 解决方案 > 在颤动中传递资产图像时显示错误

问题描述

我只是一个颤抖的初学者。我正在尝试将资产图像传递给其他类,但出现错误。这是我的代码

main.dart 文件

        
     class MyApp extends StatelessWidget {
       @override
        Widget build(BuildContext context) {
        return MaterialApp(
        home: Scaffold(
        appBar: AppBar(
          title: Text('Home'),
        ),
        body: ListView(
          children: <Widget>[
             ListInfo(photo:"assets\images\apple.jpg",name:"Apple");
                         ],
                       ),
                     ),
                   );
                 }
               }

列表信息类

     class ListInfo
     {
     final String photo;
     final String name;
     ListInfo({this.photo, this.name});
     }

列表图像类

        class ListImages extends StatelessWidget {
        @override
         ListInfo info = new ListInfo();
        Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(20.0),
      child: Center(
        child: Column(
          children: <Widget>[
            Image(
              image: AssetImage(info.photo),
            ),
            Text(
              info.name,
              style: TextStyle(fontSize: 30.0),
            ),
          ],
        ),
      ),
    );
  }
}

抱歉问了这么一个基本的问题。但我无法解决这个问题

标签: flutter

解决方案


添加资产pubspec.yaml

# To add assets to your application, add an assets section, like this:
  # assets:
  #   - images/a_dot_burr.jpeg
  #   - images/a_dot_ham.jpeg

 assets:
  -assets\images\apple.jpg
  -assets\images\orange.jpg

然后更新pub get

用于Image.asset('assets\images\apple.jpg')将图像作为小部件添加到列表中

return Container(
  padding: EdgeInsets.all(20.0),
  child: Center(
    child: Column(
      children: <Widget>[
        Image.asset('assets\images\apple.jpg',),
        Text(
          info.name,
          style: TextStyle(fontSize: 30.0),
        ),

        Image.asset('assets\images\orange.jpg',),
        Text(
          info.name,
          style: TextStyle(fontSize: 30.0),
        ),
      ],
    ),
  ),
);

推荐阅读