![](/img/trans.png)
[英]Error: Null check operator used on a null value - for boolean value - in Flutter
[英]Null check operator used on a null value error in flutter
如何解决?
环境:sdk:">=2.12.0 <3.0.0"
这是一段具有空安全错误的代码,我试图从我的数据库中获取数据以显示为轮播滑块
FutureBuilder(
future: getSlider,
builder: (BuildContext context,
AsyncSnapshot<List<ModelEbook>> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
//Create design in here
return SizedBox(
height: 27.0.h,
child: Swiper(
autoplay: true,
itemCount: snapshot.data!.length,
itemBuilder: (BuildContext context, int index) {
return GestureDetector(
onTap: () {},
child: Padding(
padding: EdgeInsets.all(10),
child: Container(
child: Stack(
children: [
ClipRRect(
child: Image.network(
listSlider[index].photo,
fit: BoxFit.cover,
width: 100.0.w,
),
borderRadius:
BorderRadius.circular(15),
),
],
),
),
),
);
},
),
);
} else {
return Container();
}
},
),
下面是调试控制台中加载的错误
════════ Exception caught by widgets library ═══════════════════════════════════
The following _CastError was thrown building FutureBuilder<List<ModelEbook>>(dirty, state: _FutureBuilderState<List<ModelEbook>>#8fb37):
Null check operator used on a null value
The relevant error-causing widget was
FutureBuilder<List<ModelEbook>>
When the exception was thrown, this was the stack
#0 _HomeState.build.<anonymous closure>.
<anonymous closure>
#1 _FutureBuilderState.build
#2 StatefulElement.build
#3 ComponentElement.performRebuild
#4 StatefulElement.performRebuild
#5 Element.rebuild
#6 BuildOwner.buildScope
#7 WidgetsBinding.drawFrame
#8 SchedulerBinding._invokeFrameCallback
#9 SchedulerBinding.handleDrawFrame
(elided 3 frames from dart: async)
================================================= ==============================
您需要检查snapshot.data
。 这应该是这样的:
if (snapshot.connectionState == ConnectionState.done) {
//Create design in here
if (snapshot.data == null) {
return Text('no data');
} else
return SizedBox(
height: 27.0.h,
child: Swiper(
autoplay: true,
itemCount: snapshot.data!.length,
itemBuilder: (BuildContext context, int index) {
/// ....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.