skip to Main Content

I’m working on a Flutter project that uses the carousel_slider package. After updating my dependencies, I encountered the following error:

‘CarouselController’ isn’t a function.
Try correcting the name to match an existing function, or define a method or function named ‘CarouselController’.
The name ‘CarouselController’ is defined in the libraries ‘package:carousel_slider/carousel_controller.dart’ and ‘package:flutter/src/material/carousel.dart (via package:flutter/material.dart)’.
Try using ‘as prefix’ for one of the import directives, or hiding the name from all but one of the imports.

It seems there’s a naming conflict between CarouselController from the carousel_slider package and another CarouselController from Flutter’s material library.

You can resolve this by giving one of the imports an alias. This way, you can specify which CarouselController you’re referring to when you use it in your code.


import 'package:carousel_slider/carousel_controller.dart' as slider;
import 'package:carousel_slider/carousel_options.dart';
import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';

class DestinationCarousel extends StatefulWidget {
  @override
  _DestinationCarouselState createState() => _DestinationCarouselState();
}

class _DestinationCarouselState extends State<DestinationCarousel> {
  final slider.CarouselController _controller = slider.CarouselController();

  // Rest of the code...
}

3

Answers


  1. The controller from the carosuel_slider is CarouselSliderController. you don’t have to use prefix

    import 'package:carousel_slider/carousel_slider.dart';
    import 'package:flutter/material.dart';
    ....
    
      final CarouselSliderController _controller = CarouselSliderController();
    
    
    Login or Signup to reply.
  2. I think you are using old versio of carousel_slider they have changed the name of controller to CarouselSliderController use latest version of carousel_slider or hide the CraouselController from the material library like this:

    import 'package:flutter/material.dart' hide CarouselController;
    

    The hiding might not solve all the issues therefore it is recommended to use the lateast version of the carousel_slider.

    Login or Signup to reply.
  3. To resolve the issue, simply update carousel_slider to version 5.0.0 or later in your project’s pubspec.yaml:

    carousel_slider: ^5.0.0
    

    In this update, they have renamed CarouselController to CarouselSliderController, which should fix the conflict.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search