0

I'm using carousel_slider to change between my screen pages as a carousel, but when I click a button to change to another screen I use the method animateToPage from a CarouselController to change to the new screen -> controller.animateToPage(pageIndex)

  void handleButtonClick() {
    if (pageIndex != 1 && pageIndex != 2) {
      goToLogin!.call();
    } else {
      controller.animateToPage(pageIndex);
    }
  }

Every time I run the app it works perfectly, but if a try a hot restart or a hot reload I get the following error when clicking the button

E/flutter ( 5497): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Null check operator used on a null value E/flutter ( 5497): #0 CarouselControllerImpl.animateToPage (package:carousel_slider/carousel_controller.dart:106:41) E/flutter ( 5497): #1 PresentationButton.handleButtonClick....

Does someone know how to solve this problem?

Gwhyyy
  • 7,554
  • 3
  • 8
  • 35
Gustavo F.
  • 95
  • 13

2 Answers2

0

check whether pageIndex is null or not.

If it's null, try to initialize with a non null value like int pageIndex = 0

Harish
  • 649
  • 5
  • 11
  • I used pageIndex = 0 and even though the error persisted. The only way I found to stop it was by changing the Stateless class to a Stateful class – Gustavo F. Mar 18 '22 at 15:11
0

I was facing the same issue: my carousel slider was working perfect, but after I updated the state, the controller functions were not longer working.

As @GustavoF mentioned, I solved the problem by declaring the CarouselController in the State of an StatefulWidget, then the build method will use this same controller as the parameter for the CarouselSlider