簡體   English   中英

使用 package CarouselSlider 時出現錯誤,它顯示錯誤類型“圖像”不是 flutter 中“字符串”類型的子類型

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

它顯示來自網絡的 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,),
              ],
            ),
        ),

您必須在列表中設置 url 而不是 Image Widgets。 將您的列表更改為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM