【问题标题】:Null check operator used on a null value error in flutter [duplicate]空值检查运算符用于颤振中的空值错误[重复]
【发布时间】:2021-10-19 19:52:41
【问题描述】:

如何解决?

环境: sdk: ">=2.12.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)

════════════════════════════════════════════════ ═════════════════════════════════

【问题讨论】:

  • 你能在getSlider中展示你在做什么吗?
  • 您的snapshot.data 可能为空。除了ConnectionState.done,请始终检查snapshot.hasData 是否为真,以检查您是否有来自已完成连接的非空返回值。

标签: flutter dart dart-null-safety


【解决方案1】:

您需要查看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) {
                            /// ....

【讨论】:

  • 它显示 'text no' 数据而不是从 db 获取它
猜你喜欢
  • 2022-01-19
  • 2021-11-17
  • 2021-07-08
  • 2021-09-25
  • 2022-07-20
  • 2021-08-12
  • 2022-09-30
  • 1970-01-01
  • 2021-11-04
相关资源
最近更新 更多