使用软件包CarouselSlider时出现错误,在flutter中显示错误类型'Image'不是'String'类型的子类型

时间:2019-11-18 13:28:14

标签: flutter dart carousel

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

从我的flutter项目中获取的图像列表:

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,),
              ],
            ),
        ),

1 个答案:

答案 0 :(得分:1)

您必须在列表中设置网址,而不是图像小部件。将您的列表更改为:

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