I'm using a Swiper package to achieve a carousel effect on my images. I'm trying to update the current index of my Swiper by passing a callback function to it's child.

but when I try to call the function, it returns this " scrollcontroller not attached " error.

I've added a SwiperController but still the same.

Here is my code:

SwiperController swiperController;

  Widget build(BuildContext context) {
    return Container(
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
        color: Colors.black,
        child: Swiper(
          controller: swiperController,
          index: _index,
          scrollDirection: Axis.horizontal,
          itemBuilder: (BuildContext c, int i) {
            return StoriesPerUser(
              storiesList: widget.storiesList,
              selectedIndex: i,
              updateFunction: callBack,
          itemCount: widget.storiesList.length,
          loop: false,
          duration: 1000,

  callBack() {
    setState(() {

Please help.

Solution 1: Yster


It seems that SwiperController was not instantiated and initialised. You can do it by overriding the initState method:

void initState() {
  controller = SwiperController();
  controller.length = 10
  //controller.fillRange(0, 10, SwiperController());

Solution 2: Gentle


If any of you guys want to use this package, and if you want a feature similar to mine, instead of updating the index, just use the one of the methods of SwiperController which is next().

this solved my problem:

callBack() {
    setState(() {