简体   繁体   中英

I am getting error while using package CarouselSlider it display error type 'Image' is not a subtype of type 'String' in flutter

It shows url images from network but doesnot shows images from assests asI am trying to access image from assests.

list of images from assests inside my flutter project:

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

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

You have to set the urls in your list instead of Image Widgets. Change your List to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM