首页 > 解决方案 > 如果我添加另一个小部件,则出现死代码或空问题

问题描述

片段有点长,我真的很抱歉


Widget customListTile3(List<Datum> articles, int index, BuildContext context) {
  final urlImages = [
    articles[0].imageUrl!,
    articles[1].imageUrl!,
    articles[2].imageUrl!,
    articles[3].imageUrl!,
    articles[4].imageUrl!,
    articles[5].imageUrl!,
    articles[6].imageUrl!,
    articles[7].imageUrl!,
    articles[8].imageUrl!,
    articles[9].imageUrl!,
  ];

  String? time = DateFormat(
    'd MMMM yy',
  ).format(
    DateTime.parse(
      '${articles[index].date!}'.toString(),
    ),
  );
  Container(
    // padding: const EdgeInsets.all(20.0),
    margin: const EdgeInsets.all(6),
    decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(5.0),
        boxShadow: const [
          BoxShadow(color: Colors.white, blurRadius: 1, offset: Offset(0, 0)),
        ]),
    child: Column(
      children: <Widget>[
        Center(
          child: CarouselSlider.builder(
            options: CarouselOptions(
              viewportFraction: 1,
              // autoPlay: true,
            ),
            itemCount: 10,
            itemBuilder: (context, idx, realIndex) {
              return buildImage(articles, urlImages[idx], idx, context);
            },
          ),
        ),
      ],
    ),
  );
}

@override
Widget buildImage(articles, String urlImage, int idx, context) {
  return Container(
      margin: const EdgeInsets.all(2),
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(5.0),
          boxShadow: const [
            BoxShadow(
                color: Colors.black12, blurRadius: 5, offset: Offset(0, 3)),
          ]),
      child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
        Stack(alignment: Alignment.center, children: [
          GestureDetector(
            child: Container(
              height: 196,
              width: double.infinity,
              decoration: BoxDecoration(
                image: DecorationImage(
                  fit: BoxFit.cover,
                  // alignment: FractionalOffset.center,
                  image: NetworkImage(articles[idx].imageUrl!),
                ),
                borderRadius: const BorderRadius.only(
                    topLeft: Radius.circular(5),
                    topRight: Radius.circular(5),
                    bottomRight: Radius.circular(5),
                    bottomLeft: Radius.circular(5)),
              ),
              child: Container(
                margin: const EdgeInsets.only(top: 120, left: 0),
                color: Colors.black45.withOpacity(0.4),
                padding: const EdgeInsets.all(10.0),
                child: Text(
                  '${articles[idx].title}',
                  textAlign: TextAlign.left,
                  softWrap: true,
                  maxLines: 2,
                  overflow: TextOverflow.ellipsis,
                  style: const TextStyle(
                      color: Colors.white,
                      fontSize: 16,
                      fontWeight: FontWeight.w500,
                      fontFamily: ''),
                ),
              ),
            ),
            onTap: () {
              Icons.message;

              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => NewsViewDetail(
                    id: articles[idx].id!,
                  ),
                ),
              );
            },
          ),
        ])
      ]));

  Column(
    children: [
      DotsIndicator(
        dotsCount: 10,
        position: idx.toDouble(),
        decorator: DotsDecorator(
          size: const Size.square(6.0),
          activeSize: const Size(22.0, 6.0),
          activeShape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(5.0)),
          activeColor: Colors.blue,
          spacing: const EdgeInsets.all(3.0),
        ),
      ),
    ],
  );
}

问题是我有点指示器,我想在滑块容器下使用它。如果我在容器关闭后放置 DotsIndicator,我会收到死代码错误。

如果我从第一个容器中删除返回,这次我会收到 null 安全错误。我该如何处理?

标签: flutterdart

解决方案


您的最后一列不在构建方法中更改此

      ]));

  Column(
    children: [
      DotsIndicator(
        dotsCount: 10,
        position: idx.toDouble(),
        decorator: DotsDecorator(
          size: const Size.square(6.0),
          activeSize: const Size(22.0, 6.0),
          activeShape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(5.0)),
          activeColor: Colors.blue,
          spacing: const EdgeInsets.all(3.0),
        ),
      ),
    ],
  );

在这

  Column(
    children: [
      DotsIndicator(
        dotsCount: 10,
        position: idx.toDouble(),
        decorator: DotsDecorator(
          size: const Size.square(6.0),
          activeSize: const Size(22.0, 6.0),
          activeShape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(5.0)),
          activeColor: Colors.blue,
          spacing: const EdgeInsets.all(3.0),
        ),
      ),
    ],
  )]));

我的意思是空安全错误,因为您使用(articles, String urlImage, int idx)if-else如何notEmpty将参数更改为可为空的(dynamic articles, String? urlImage, int? idx)


推荐阅读