Skip to content

fix: ScrollController is disposed when parent widget state changes (#289) #333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

lhengl
Copy link
Contributor

@lhengl lhengl commented May 14, 2025

Description

This PR fixes a lifecycle issue where ChatListWidget improperly disposes the ScrollController provided via the external ChatController.

Currently, ChatController.scrollController is passed into the widget tree and used in multiple places, including pagination and scrolling logic. However, the ChatListWidget calls scrollController.dispose() in its dispose() method — even though it did not create the controller.

This causes runtime exceptions (ScrollController was disposed) when parent widgets rebuild and reuse the same controller, which is a valid and supported use case.

This PR changes:

  • Removes the call to scrollController.dispose() and chatController.messageStreamController.close() from ChatListWidget.dispose()
  • Ensures that the responsibility of disposing shared resources remains with the owning class (i.e., ChatController)

No behavior changes, just safer lifecycle management.

Checklist

  • The title of my PR starts with a [Conventional Commit] prefix (fix:).
  • I have followed the [Contributor Guide] when preparing my PR.
  • I have updated/added tests for ALL new/updated/fixed functionality.
  • I have updated/added relevant documentation in docs and added dartdoc comments with ///.
  • I have updated/added relevant examples in examples or docs.

(Tests/docs not updated because the fix affects lifecycle behavior, not exposed API.)

Breaking Change?

  • Yes, this PR is a breaking change.
  • No, this PR is not a breaking change.

Related Issues

N/A, but may be related to general lifecycle issues encountered by users embedding ChatView in reactive widget trees (e.g. GetX, Riverpod).

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR addresses a lifecycle issue by removing the disposal of shared resources (ScrollController and messageStreamController) from ChatListWidget to prevent runtime exceptions when reused externally.

  • Removed calls to dispose shared controllers in ChatListWidget.
  • Updated CHANGELOG to document the lifecycle fix.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
lib/src/widgets/chat_list_widget.dart Removed disposal calls for shared ScrollController and messageStreamController
CHANGELOG.md Updated changelog to reflect the fix for issue #289
Comments suppressed due to low confidence (2)

lib/src/widgets/chat_list_widget.dart:247

  • Consider adding a comment to clarify that the disposal of messageStreamController is handled by ChatController, explaining that this prevents double disposal when the controller is shared.
chatController.messageStreamController.close();

lib/src/widgets/chat_list_widget.dart:247

  • Consider adding a comment indicating that scrollController is managed externally by ChatController to provide clarity on the lifecycle ownership of this resource.
scrollController.dispose();

@lhengl
Copy link
Contributor Author

lhengl commented May 30, 2025

Can someone from @SimformSolutionsPvtLtd advise how we can move this PR forward please? It's only a simple fix. @dhyash-simform?

Copy link
Collaborator

@aditya-css aditya-css left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution! This looks good to me, but, can you please add chatController.dispose in the main.dart file in the example project?

That would make it clear the chatController should be disposed and set the standard practice.

@aditya-css
Copy link
Collaborator

Hey @lhengl, Can you please resolve conflict and address the comment I shared?

lhengl added 2 commits June 3, 2025 07:33
…scroll-controller

# Conflicts:
#	CHANGELOG.md
#	example/lib/main.dart
@lhengl
Copy link
Contributor Author

lhengl commented Jun 2, 2025

Hi @aditya-css, I've added the initState and dispose state as per your request. And I've resolved the merge conflict as well. Let me know if you need anything further.

@aditya-css aditya-css merged commit 5e6599e into SimformSolutionsPvtLtd:main Jun 3, 2025
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.

2 participants