1

我正在为carousel_slider 4.0.0使用我的颤振(空安全版本)应用程序,滑块工作但是,我遇到了这个错误

类型“Null”不是类型转换中“List”类型的子类型

有什么解决方案吗?

在此处输入图像描述 我的代码在这里

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter_svg/svg.dart';
import 'package:carousel_slider/carousel_slider.dart';
import 'package:rmapp/provider/theme_provider.dart';

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  //swiper current index

  late final List<String> imageList = [
    "https://www.setaswall.com/wp-content/uploads/2018/08/Spiderman-Wallpaper-76-1280x720.jpg",
    "https://lh3.googleusercontent.com/proxy/yL2FmQfZA79S5eIDza9MH2NjKGIKWPOGRHxHdYwiNPcYDW26YmK6qnP01ZDLsBENZpiADc1ohkj3LzVjrwoX8Pb-crT6MYZb3Jp9gy3ZrlET_yvoFS0qtUHLq4DtVPcqIdxPiNWI_j08omBVACv-YJc",

  ];
  @override
  Widget build(BuildContext context) {
    Provider.of<ThemeProvider>(context).themeMode == ThemeMode.dark
        ? 'DarkTheme'
        : 'LightTheme';
    return Scaffold(
      // Setting up AppBar
      appBar: AppBar(
        automaticallyImplyLeading: false,
        iconTheme: Theme.of(context).iconTheme,

       

      ),

      // Setting up Background Color


      // Body
      body: Container(
        child: ListView(
          physics: ClampingScrollPhysics(),
          children: <Widget>[

            Padding(
              padding: EdgeInsets.only(left: 16, bottom: 10, top: 10,),
              child: Text(
                'Hi, Welcome back!',
                //style: mTitleStyle,
              ),
            ),
            //Promo Slider
            _promoSlider(),

          ],
        ),
      ),
    );
  }


  //Promo Slider
  Widget _promoSlider() {
    return Container(



            child: CarouselSlider(
              options: CarouselOptions(
                enlargeCenterPage: true,
                enableInfiniteScroll: false,
                autoPlay: true,
              ),
              items: imageList.map((e) => ClipRRect(
                borderRadius: BorderRadius.circular(8),
                child: Stack(
                  fit: StackFit.expand,
                  children: <Widget>[
                    Image.network(e,
                      width: 1050,
                      height: 350,
                      fit: BoxFit.cover,)
                  ],
                ) ,
              )).toList(),
                ),);

  }

}
4

3 回答 3

1

此错误可能表明您的某些imageList内容为空。其属性CarouselSlider中的键/值需要非空参数。items

于 2021-07-20T09:56:39.867 回答
0

遇到同样的问题。虽然删除坏链接可以解决问题,但它并不能防止问题在没有人工干预的情况下再次发生。所以我最终写了一小段代码,在将它传递给 Carousel Slider 之前基本上清理了 imageList。这是相关的片段:

import 'package:http/http.dart' as http;

Future<bool> photoExists(String photourl) async {
        final response = await http.get(Uri.parse(photourl));
        if (response.statusCode == 200) {
          return true;
        } else {
          return false;
        }
      }

List imageList = original_imageList;  // replace original_imageList with the appropriate list of urls
    for (String singleImage in imageList) {
      photoExists(singleImage).then((value) {
        if (!value) {
          imageList.remove(singleImage);
        }
      });
    } 

现在您可以在项目中使用 imageList:Carousel Slider 的属性

注意:由于这是未来,您可能会在控制台中收到 null 错误消息,但您不会在 Carousel Slider 中看到“null”图像

于 2021-09-11T16:41:19.253 回答