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
The controller from the carosuel_slider is
CarouselSliderController
. you don’t have to use prefixI think you are using old versio of
carousel_slider
they have changed the name of controller toCarouselSliderController
use latest version ofcarousel_slider
or hide the CraouselController from the material library like this:To resolve the issue, simply update carousel_slider to version 5.0.0 or later in your project’s pubspec.yaml:
In this update, they have renamed
CarouselController
toCarouselSliderController
, which should fix the conflict.