Skip to content

fix: prevent null crash in auto-play timer when pageController.page - #495

Open
laksh-scapia wants to merge 2 commits into
serenader2014:masterfrom
scapia:fix/auto-scroll-crash-fix
Open

fix: prevent null crash in auto-play timer when pageController.page#495
laksh-scapia wants to merge 2 commits into
serenader2014:masterfrom
scapia:fix/auto-scroll-crash-fix

Conversation

@laksh-scapia

Copy link
Copy Markdown

fix: prevent null crash in auto-play timer when pageController.page is null

Problem

The auto-play timer callback crashed with Null check operator used on a null value at carousel_slider.dart:144:

int nextPage = carouselState!.pageController!.page!.round() + 1;

Two compounding bugs caused this:

Bug 1 — wrong dispose() order

void dispose() {
  pageController?.dispose(); // detaches PageView → page becomes null
  super.dispose();
  clearTimer();              // timer still live during the window above
}

super.dispose() synchronously detaches the PageController from the PageView, making pageController.page return null. The timer was not cancelled until after that, leaving a window where the callback could fire against a detached controller.

Bug 2 — unsafe null-unwrap of pageController.page

pageController.page is a double? — it returns null whenever the controller is not attached to a scrollable. This can happen after super.dispose() runs, and also during the window in didUpdateWidget where a brand-new PageController is assigned to carouselState!.pageController before the widget rebuilds. The existing !mounted guard does not protect against either case.


Fix

1. Swap dispose() order — cancel the timer before any Flutter teardown:

void dispose() {
  clearTimer();
  pageController?.dispose();
  super.dispose();
}

2. Guard page before unwrapping — treat a null page as a bail-out signal, layered alongside the existing !mounted guard:

final double? currentPage = carouselState!.pageController!.page;
if (currentPage == null) return;
int nextPage = currentPage.round() + 1;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant