flutter_carousel_slider icon indicating copy to clipboard operation
flutter_carousel_slider copied to clipboard

NoSuchMethodError: The getter 'options' was called on null.

Open hagen00 opened this issue 4 years ago • 4 comments

Getting the following error

E/flutter (16405): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: NoSuchMethodError: The getter 'options' was called on null.
E/flutter (16405): Receiver: null
E/flutter (16405): Tried calling: options
E/flutter (16405): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
E/flutter (16405): #1      CarouselControllerImpl.nextPage (package:carousel_slider/carousel_controller.dart:53:42)

Using this code:

Stack(
          alignment: Alignment.topCenter,
          fit: StackFit.loose,
          children: <Widget>[
            Builder(
              builder: (context) {
                final double height = MediaQuery.of(context).size.height;
                return CarouselSlider.builder(
                  carouselController: carouselController,
                  options: CarouselOptions(
                    height: height,
                    autoPlay: false,
                    enlargeCenterPage: true,
                    viewportFraction: 1,
                    initialPage: 0,
                  ),
                  itemCount: pages.length,
                  itemBuilder: (context, index) => pages[index], // final List<Widget> pages = [Foo(), Bar(), FooBar()];
                );
              },
            ),
            Padding(
              padding: const EdgeInsets.all(20.0),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  IconButton(
                    icon: Icon(Icons.chevron_left),
                    onPressed: () => carouselController.previousPage(duration: Duration(milliseconds: 300), curve: Curves.linear),
                  ),
                  IconButton(
                    icon: Icon(Icons.chevron_right),
                    onPressed: () => carouselController.nextPage(duration: Duration(milliseconds: 300), curve: Curves.linear),
                  ),
                ],
              ),
            ),
          ],
        )

hagen00 avatar Aug 28 '20 09:08 hagen00

Is there any solution about this issue?

sgelves avatar Sep 13 '20 07:09 sgelves

@Selecao found an amazing solution in https://github.com/serenader2014/flutter_carousel_slider/issues/175#issuecomment-657736568_

AhmedAbouelkher avatar Sep 21 '20 16:09 AhmedAbouelkher

Hello. I have done this but doesn't work still:

CarouselSlider(
                    items: imageSliders,
                    carouselController: carouselController,
                    options: CarouselOptions(
                        autoPlay: true,
                        autoPlayInterval: Duration(seconds: 7),
                        enlargeCenterPage: true,
                        aspectRatio: 1.5,
                        viewportFraction: 1.0,
                        onPageChanged: (index, reason) {
                          setState(() {
                            _current = index;
                          });
                        }),
                  ),

billishe avatar Oct 19 '20 07:10 billishe

I fix this, by adding key to Carousel Slider.

CarouselSlider(
  key: ValueKey(carouselController),
  carouselController: carouselController,
),

bohdansushchak avatar Feb 14 '23 12:02 bohdansushchak