Skip to content

Conversation

@krushnarout
Copy link
Member

@krushnarout krushnarout commented Jan 3, 2026

_scrollController.position.maxScrollExtent,

ScrollController had no attached position because the ListView was not built yet, that’s whyu position became invalid

closes #4023

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This review identified a potential memory leak due to an undisposed _scrollController and a critical race condition in the scrollDown method. These findings highlight important areas for improving the stability and performance of the application.

Comment on lines 82 to -89
void scrollDown() async {
if (_scrollController.hasClients) {
await Future.delayed(const Duration(milliseconds: 250));
_scrollController.animateTo(
_scrollController.position.maxScrollExtent,
duration: const Duration(milliseconds: 200),
curve: Curves.easeOut,
);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

There is a race condition here. The _scrollController.hasClients check is performed before an await. After the Future.delayed completes, the widget might have been disposed, and _scrollController might no longer have clients. Accessing _scrollController.position would then throw an exception. The checks for mounted and hasClients (or positions.isNotEmpty) should be performed after any await calls to ensure the state is still valid before using the controller.

Comment on lines 74 to 77
void dispose() {
_scrollController.dispose();
_questionAnimationController.dispose();
super.dispose();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The _scrollController is not being disposed of in the dispose method. This can lead to memory leaks. It should be disposed of by calling _scrollController.dispose() to release its resources when the State object is removed from the tree permanently.

@beastoin
Copy link
Collaborator

so _scrollController.hasClients does not work?

@krushnarout
Copy link
Member Author

krushnarout commented Jan 10, 2026

so _scrollController.hasClients does not work?

hasClients only checks that the controller is attached at that moment. Since scrollDown waits before running, the list can detach in between so accessing position will crash

@krushnarout
Copy link
Member Author

@aaravgarg

@aaravgarg aaravgarg requested a review from mdmohsin7 January 17, 2026 14:48
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.

FlutterError - Bad state: No element - _SpeechProfilePageState.scrollDown

3 participants