我已经面临这个问题很长一段时间了,现在我刚接触到flutter和用于Null的输出显示Null检查操作符。
在构建StreamBuilder(dirty, state: _StreamBuilderBaseState<articlerresponse,>#39173)时抛出以下_CastError:用于空值 的空检查操作符import 'package:carousel_slider/carousel_slider.dart';
import 'package:firstapp/bloc/get_top_headlines_bloc.dart';
import 'package:firstapp/elements/error_element.dart';
import 'package:firstapp/elements/loader_element.dart';
import 'package:firstapp/model/article.dart';
import 'package:firstapp/model/article_response.dart';
import 'package:flutter/material.dart';
import 'package:timeago/timeago.dart' as timeago;
class HeadlineSliderWidget extends StatefulWidget {
const HeadlineSliderWidget({Key? key}) : super(key: key);
@override
_HeadlineSliderWidgetState createState() => _HeadlineSliderWidgetState();
}
class _HeadlineSliderWidgetState extends State<HeadlineSliderWidget> {
@override
void initState() {
super.initState();
getTopHeadlinesBloc..getHeadlines();
}
@override
Widget build(BuildContext context) {
return StreamBuilder<ArticleResponse>(
stream: getTopHeadlinesBloc.subject.stream,
builder: (context, AsyncSnapshot<ArticleResponse> snapshot) {
if (snapshot.hasData) {
if (snapshot.data?.error != null && snapshot.data!.error.length > 0) {
return buildErrorWidget(snapshot.data!.error);
}
return _buildHeadlineSliderWidget(snapshot.data!);
}else if (snapshot.hasError){
return buildErrorWidget(snapshot.data!.error);
} else {
return buildLoadingWidget();
}
// if (snapshot.hasData) {
// return _buildHeadlineSliderWidget(snapshot.data!);
// } else if (!snapshot.hasError) {
// return buildErrorWidget(snapshot.data!.error);
// } else {
// return buildLoadingWidget();
// }
},
);
}
// ignore: unrelated_type_equality_checks
// if (snapshot.hasError != snapshot.data!.error.length > 0) {
// return buildErrorWidget(snapshot.data!.error);
// }
// var data = snapshot.data!;
// return _buildHeadlineSliderWidget(data);
// } else if (snapshot.hasError) {
// return buildErrorWidget(snapshot.data!.error);
// } else {
// return buildLoadingWidget();
Widget _buildHeadlineSliderWidget(ArticleResponse data) {
List<ArticleModel> articles = data.articles;
return Container(
child: CarouselSlider(
options: CarouselOptions(
enlargeCenterPage: false, height: 200.0, viewportFraction: 0.9),
items: getExpenseSliders(articles),
),
);
}
getExpenseSliders(List<ArticleModel> articles) {
return articles
.map(
(article) => GestureDetector(
onTap: () {},
child: Container(
padding: const EdgeInsets.only(
left: 5.0, right: 5.0, top: 10.0, bottom: 10.0),
child: Stack(
children: <Widget>[
Container(
decoration: new BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(8.0)),
shape: BoxShape.rectangle,
image: new DecorationImage(
fit: BoxFit.cover,
// ignore: unrelated_type_equality_checks
image: article.img != String
? NetworkImage(article.img!)
: AssetImage("assets/img/placeholder.jpg")
as ImageProvider),
),
),
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(8.0)),
gradient: LinearGradient(
begin: Alignment.bottomCenter,
end: Alignment.topCenter,
stops: [
0.1,
0.9
],
colors: [
Colors.black.withOpacity(0.9),
Colors.white.withOpacity(0.0)
]),
),
),
Positioned(
bottom: 30.0,
child: Container(
padding: EdgeInsets.only(left: 10.0, right: 10.0),
width: 250.0,
child: Column(
children: <Widget>[
Text(
article.title!,
style: TextStyle(
height: 1.5,
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 12.0),
),
],
),
)),
Positioned(
bottom: 10.0,
left: 10.0,
child: Text(
article.source.name!,
style: TextStyle(color: Colors.white54, fontSize: 9.0),
)),
Positioned(
bottom: 10.0,
right: 10.0,
child: Text(
timeAgo(DateTime.parse(article.date!)),
style: TextStyle(color: Colors.white54, fontSize: 9.0),
)),
],
),
),
),
)
.toList();
}
String timeAgo(DateTime date) {
return timeago.format(date, allowFromNow: true, locale: 'en');
}
}
如果你没有真正发布完整的堆栈跟踪(你总是应该这样做),你的问题很可能是从这里开始的。
article.img != String
? NetworkImage(article.img!)
: AssetImage("assets/img/placeholder.jpg") as ImageProvider),
如果你想检查一个类型,你需要做article.img is! String
或更好的,在这种情况下,只做一个空检查article.img != null
。
,因为现在你的三进制语句将始终计算为true
,因为article.img
与String
类不同。因此,当您执行article.img!
且img为null时,将抛出此错误
通过使用!
操作符,你对编译器说"这个变量永远不会为空">