Reputation: 1587
I am getting above error while using addListener
on Scroll Controller.
Class is initialised once but the method is called multiple times and where the error is occurred.
I have declared controller globally like
ScrollController rwScrollController = ScrollController();
using inside the method like
fetchRewardLadder(
String code, {
bool fromFab = false,
}) async {
rwScrollController = ScrollController(); //----> initialised
GccApiResponse response = await _repository.fetchRewardLadder(
Variables$Query$fetchRewardLadder(
code: code,
),
);
switch (response.status) {
case Status.ERROR:
showExceptionSnackbar(message: response.message);
break;
default:
try {
Query$fetchRewardLadder$fetchRewardLadder reward =
Query$fetchRewardLadder$fetchRewardLadder.fromJson(
response.data["fetchRewardLadder"],
);
safeEmit(
state.copyWith(
rewardLadder: reward,
isLoading: false,
),
);
rwScrollController.addListener(
() {
//--------- getting error here ----------------
if (rwScrollController.position.pixels >
(0.95 * rwScrollController.position.maxScrollExtent) &&
!state.isLoading &&
state.leaderboard == null) {
fetchLeaderBoard(code);
}
},
);
} catch (e) {
showExceptionSnackbar(
message: "Formatting failed" + e.toString(),
);
}
}
}
full crash logs :
Fatal Exception: io.flutter.plugins.firebase.crashlytics.FlutterError: Bad state: Too many elements. Error thrown Instance of 'ErrorDescription'. at _GrowableList.single(dart:core) at ScrollController.position(scroll_controller.dart:109) at RewardLadderCubit.fetchRewardLadder.(reward_ladder_cubit.dart:136) at ChangeNotifier.notifyListeners(change_notifier.dart:381) at ChangeNotifier.notifyListeners(change_notifier.dart:381) at ScrollPosition.notifyListeners(scroll_position.dart:984) at ScrollPosition.setPixels(scroll_position.dart:281) at ScrollPositionWithSingleContext.setPixels(scroll_position_with_single_context.dart:78) at ScrollPositionWithSingleContext.applyUserOffset(scroll_position_with_single_context.dart:122) at ScrollDragController.update(scroll_activity.dart:387) at ScrollableState._handleDragUpdate(scrollable.dart:719) at DragGestureRecognizer._checkUpdate.(monodrag.dart:483) at GestureRecognizer.invokeCallback(recognizer.dart:253) at DragGestureRecognizer._checkUpdate(monodrag.dart:483) at DragGestureRecognizer.handleEvent(monodrag.dart:330) at PointerRouter._dispatch(pointer_router.dart:98) at PointerRouter._dispatchEventToRoutes.(pointer_router.dart:143) at _LinkedHashMapMixin.forEach(dart:collection) at PointerRouter._dispatchEventToRoutes(pointer_router.dart:141) at PointerRouter.route(pointer_router.dart:127) at GestureBinding.handleEvent(binding.dart:460) at GestureBinding.dispatchEvent(binding.dart:440) at RendererBinding.dispatchEvent(binding.dart:336) at GestureBinding._handlePointerEventImmediately(binding.dart:395) at GestureBinding.handlePointerEvent(binding.dart:357) at GestureBinding._flushPointerEventQueue(binding.dart:314) at GestureBinding._handlePointerDataPacket(binding.dart:295)
Upvotes: -1
Views: 1178
Reputation: 1587
Problem was that whenever I was declaring it globally flutter engine was not disposing the widget as cubit was in use so whenever it re-renders it reassign new scrollController to same listview thus causing error. So I created a new method and used it each time widget re-renders.
ScrollController getRWController() {
ScrollController rwScrollController = ScrollController();
rwScrollController.addListener(
() {
if (rwScrollController.position.pixels >
(0.95 * rwScrollController.position.maxScrollExtent) &&
!state.isLoading &&
state.leaderboard == null) {
fetchLeaderBoard(this.code);
}
},
);
return rwScrollController;
}
and used it in listview as
ListView(
controller: context.read<RewardLadderCubit>().getRWController(),
children: [],
),
Upvotes: 0