1

How to resolve it?

environment: sdk: ">=2.12.0 <3.0.0"

This is the piece of code having a null safety error here I am trying to fetch data from my database to display as a carousel slider

 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();
                    }
                  },
                ),

below is the error loaded in debug console

════════ 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)

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

enter image description here

jasmine
  • 149
  • 13
  • Can you show what you are doing in `getSlider`? – Shaan Mephobic Oct 19 '21 at 19:56
  • 3
    Probably your `snapshot.data` is null. Besides `ConnectionState.done`, always check `snapshot.hasData` is true to examine whether you have a non-null return value from the completed connection. – Peter Koltai Oct 19 '21 at 19:57

1 Answers1

3

You need to check snapshot.data. This should look like:

                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) {
                            /// ....
Akif
  • 7,098
  • 7
  • 27
  • 53