flutter - 如果我添加另一个小部件,则出现死代码或空问题
问题描述
片段有点长,我真的很抱歉
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 安全错误。我该如何处理?
解决方案
您的最后一列不在构建方法中更改此
]));
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)
推荐阅读
- python - EarlyStopping 基于 TF/Keras 中可训练变量的收敛
- vba - 在 Word 查找/替换宏中使用 VBA 正则表达式?
- javascript - 反应在手风琴中添加图像
- java - 如何在基于 Maven 的 Java 应用程序中处理多个版本的 jar?
- date - 游戏中奇怪的 64 位时间格式,你能认出来吗?
- javascript - 如何正确使用对象的解构
- javascript - React:告诉子组件“重新初始化”,即使传递的道具相同
- python-3.x - Python3 for 循环,范围在 self 函数调用期间给出意外结果
- javascript - 使用 fetch 将数据发送到烧瓶
- javascript - 遍历树虫