首页 > 解决方案 > 我在使用包 CarouselSlider 时遇到错误,它显示错误类型“图像”不是颤振中“字符串”类型的子类型

问题描述

它显示来自网络的 url 图像,但不显示来自 assests 的图像,因为我正在尝试从 assests 访问图像。

我的颤振项目中来自资产的图像列表:

int _current = 0;
  final List imgList = [
    Image.asset('assests/GL.png'),
    Image.asset('assests/GL.png'),
    Image.asset('assests/GL.png'),
     ];

CarouselSlider 中的代码


CarouselSlider(
                  height: 200,
                  initialPage: 0,
                  enlargeCenterPage: true,
                  autoPlay: true,
                  reverse: false,
                  enableInfiniteScroll: true,
                  autoPlayInterval: Duration(seconds: 3),
                  autoPlayAnimationDuration: Duration(milliseconds: 2000),
                  pauseAutoPlayOnTouch: Duration(seconds: 10),
                  scrollDirection: Axis.horizontal,
                  onPageChanged: (index){
                    setState(() {
                      _current = index;
                    });
                  },


                  items: imgList.map((imgUrl){

                    return Builder(
                    builder: (BuildContext context){
                      return Container(
                      width: MediaQuery.of(context).size.width,
                        margin: EdgeInsets.symmetric(horizontal: 10.0),
                          decoration: BoxDecoration(color: Colors.green),
                        child: Image.assest(imgUrl,fit:BoxFit.fill),//error line in code
                      );
                      },
                    );

                }).toList(),),
            SizedBox(height: 20,),
              ],
            ),
        ),

标签: flutterdartcarousel

解决方案


您必须在列表中设置 url 而不是 Image Widgets。将您的列表更改为:

final List imgList = [
    'assests/GL.png',
    'assests/GL.png',
    'assests/GL.png',
];

推荐阅读