flutter_carousel_slider
flutter_carousel_slider copied to clipboard
Unhandled Exception: Null check operator used on a null value, when using carouselController in latest null safe beta .0 version
Basically I am unable to work with carouselController in the null safe beta version, the below error was thrown when I used, _controller.previousPage().
Logs
[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: Null check operator used on a null value
#0 CarouselControllerImpl.previousPage
package:carousel_slider/carousel_controller.dart:75
#1 _HomePageState.build.<anonymous closure>
package:appimagebrowser/main.dart:265
#2 _InkResponseState._handleTap
package:flutter/…/material/ink_well.dart:991
#3 GestureRecognizer.invokeCallback
package:flutter/…/gestures/recognizer.dart:182
#4 TapGestureRecognizer.handleTapUp
package:flutter/…/gestures/tap.dart:607
#5 BaseTapGestureRecognizer._checkUp
package:flutter/…/gestures/tap.dart:296
#6 BaseTapGestureRecognizer.acceptGesture
package:flutter/…/gestures/tap.dart:267
#7 GestureArenaManager.sweep
package:flutter/…/gestures/arena.dart:157
#8 GestureBinding.handleEvent
package:flutter/…/gestures/binding.dart:385
#9 GestureBinding.dispatchEvent
package:flutter/…/gestures/binding.dart:361
#10 RendererBinding.dispatchEvent
package:flutter/…/rendering/binding.dart:278
#11 GestureBinding._handlePointerEventImmediately
package:flutter/…/gestures/binding.dart:316
#12 GestureBinding.handlePointerEvent
package:flutter/…/gestures/binding.dart:280
#13 GestureBinding._flushPointerEventQueue
package:flutter/…/gestures/binding.dart:238
#14 GestureBinding._handlePointerDataPacket
package:flutter/…/gestures/binding.dart:221
#15 _rootRunUnary (dart:async/zone.dart:1370:13)
#16 _CustomZone.runUnary (dart:async/zone.dart:1265:19)
#17 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1170:7)
#18 _invoke1 (dart:ui/hooks.dart:180:10)
#19 PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:276:7)
#20 _dispatchPointerDataPacket (dart:ui/hooks.dart:96:31)
@serenader2014 Can you please update the beta version of null safety so that this issue is fixed, their was some reference of files in the Logs.
Also nullsafety is backward compatible, so it will also work with non-null safe applications, and hence you can test and publish the null safe version directly without worrying about any breakage.
change your version: carousel_slider 4.0.0-nullsafety.0
change your version: carousel_slider 4.0.0-nullsafety.0
I am already on that version but the carousel controller doesn't seem to be working.
Can be fixed by #271
Basically I am unable to work with carouselController in the null safe beta version, the below error was thrown when I used, _controller.previousPage().
Logs
Had the same issue. Using StatefulWidget will fix the issue. Previously I was using stateless widget with provider
Ok If you are also Facig this, then first upgrade to latest version.
And then please your carousel controller above the build context and not inside the build method.
Also use StatefulWidget as stated by @kishan2612.
@prateekmedia still I am getting this issue while calling animateToPage
and I am using 2.2.3
flutter version
Unhandled Exception: Null check operator used on a null value
E/flutter (27192): #0 CarouselControllerImpl.animateToPage (package:carousel_slider/carousel_controller.dart:106:41)
@singlakaran Did you placed your CarouselController inside or outside of build method?
It should be outside of the build method of Stateless or Stateful Widget.
I faced the same issue. This is how I solved it
class PlansPage extends StatefulWidget {
const PlansPage({Key? key}) : super(key: key);
@override
State<PlansPage> createState() => _PlansPageState();
}
class _PlansPageState extends State<PlansPage> {
int _currentPage = 1;
late CarouselController carouselController;
@override
void initState() {
super.initState();
carouselController = CarouselController();
}
}
Then put initialization the carouselController inside the initState method I was able to use the methods jumpToPage(_currentPage ) and animateToPage(_currentPage) etc.
I use animateToPage inside GestureDetector in onTap.
onTap: () {
setState(() {
_currentPage = pageIndex;
});
carouselController.animateToPage(_currentPage);
},
I apologize in advance if this is inappropriate.
How i solved it
Function:
int _currentIndex = 0;
int get currentIndex => _currentIndex;
set currentIndex(int value) { _currentIndex = value; }
CarouselController carouselController = CarouselController();
void init(int index) async { -----API CALL ---- _currentIndex = index; }
ui :
CarouselSlider.builder( carouselController: viewModel.carouselcontroller, options: CarouselOptions( height: MediaQuery.of(context).size.height, initialPage: viewModel.currentIndex, viewportFraction: 1.0, aspectRatio: 2.0, enlargeCenterPage: true, scrollDirection: Axis.horizontal, autoPlay: false, onPageChanged: (index, reason) { viewModel.currentIndex = index; viewModel.notifyListeners(); }), itemBuilder: (context, index, realIdx) { return _Data(viewModel.questionsList![index]); }, itemCount: viewModel.questionsList!.length, ),
Use initial page to avoid