serenader2014 / flutter_carousel_slider

A flutter carousel widget, support infinite scroll, and custom child widget.
https://pub.dev/packages/carousel_slider
MIT License
1.6k stars 582 forks source link

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

Open hagen00 opened 4 years ago

hagen00 commented 4 years ago

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),
                  ),
                ],
              ),
            ),
          ],
        )
sgelves commented 4 years ago

Is there any solution about this issue?

AhmedAbouelkher commented 4 years ago

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

billishe commented 4 years ago

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;
                          });
                        }),
                  ),
bohdansushchak commented 1 year ago

I fix this, by adding key to Carousel Slider.

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