As stated in the title, flutter carousel_slider’s CarouselController
doesn’t seem to work. When I trigger the nextPage
or jumpToPage
functions, I am met with a Unhandled Exception: Null check operator used on a null value
error.
I recreated a simple example:
ValueNotifier<List<int>> carouselList = useState([1,2,3,4,5]);
CarouselController _controller = CarouselController();
return CarouselSlider(
carouselController: _controller,
options: CarouselOptions(
height: 400.0,
),
items: carouselList.value.asMap().map((index, exerciseListItem) {
return MapEntry(index, Builder(
builder: (BuildContext context) {
return Container(
width: 400,
child: Padding(
padding: EdgeInsets.fromLTRB(20, 50, 20, 50),
child: GestureDetector(
onLongPress: (){
_controller.nextPage(duration: Duration(milliseconds: 300), curve: Curves.linear);
},
child: Container(
color: Colors.grey,
child: Text(
exerciseListItem.toString(),
style: TextStyle(fontSize: 16.0),
)
)
),
)
);
},
));
}).values.toList(),
);
I also copy pasted an even simpler example from the docs, but it also breaks.
CarouselController _controller = CarouselController();
return Column(
children: [
CarouselSlider(
items: [
Container(
width: 500,
color: Colors.grey,
child: Text(
"1",
style: TextStyle(fontSize: 16.0),
)
),
Container(
width: 500,
color: Colors.grey,
child: Text(
"2",
style: TextStyle(fontSize: 16.0),
)
),
Container(
width: 500,
color: Colors.grey,
child: Text(
"3",
style: TextStyle(fontSize: 16.0),
)
),
],
carouselController: _controller,
options: CarouselOptions(
autoPlay: false,
enlargeCenterPage: true,
viewportFraction: 0.9,
aspectRatio: 2.0,
initialPage: 2,
),
),
GestureDetector(
onTap: () => _controller.nextPage(
duration: Duration(milliseconds: 300), curve: Curves.linear),
child: Container(
height: 50,
width: 50,
child: Text('→'),
),
)
],
);