Skip to content

Fix/issue 186 dispose cancel#196

Open
apravint wants to merge 4 commits into
flutter:mainfrom
apravint:fix/issue-186-dispose-cancel
Open

Fix/issue 186 dispose cancel#196
apravint wants to merge 4 commits into
flutter:mainfrom
apravint:fix/issue-186-dispose-cancel

Conversation

@apravint

@apravint apravint commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes #186 - Prevents crash when users navigate away while LLM is streaming a response.

When a user navigates away from LlmChatView while the LLM is still streaming a response, the widget crashes with Null check operator used on a null value. This happens because the dispose() method doesn't cancel pending responses, allowing the onUpdate callback to call setState() on a disposed widget.

Changes

  • Added _pendingPromptResponse?.cancel() in dispose()
  • Added _pendingSttResponse?.cancel() in dispose()
  • Moved super.dispose() to the end to ensure proper cleanup order

Impact

This fix resolves crashes affecting 588 occurrences across 124 production users in production apps using flutter_ai_toolkit 1.0.0.

Pre-launch Checklist

… setState on disposed widget

This fix addresses flutter#186 by properly canceling both _pendingPromptResponse and _pendingSttResponse when the widget is disposed. Previously, when users navigated away while the LLM was streaming a response, the widget would crash with "Null check operator used on a null value" because dispose() didn't cancel the pending response, allowing the onUpdate callback to call setState() on a disposed widget.

Changes:
- Added _pendingPromptResponse?.cancel() in dispose()
- Added _pendingSttResponse?.cancel() in dispose()
- Moved super.dispose() to the end to ensure cleanup happens before widget disposal

Impact: Fixes crash affecting 588 occurrences across 124 production users
@apravint

Copy link
Copy Markdown
Contributor Author

The fix adds the missing cancel() calls for both _pendingPromptResponse and _pendingSttResponse in the dispose() method to prevent setState on disposed widget.

The implementation:

  1. Cancels pending LLM response stream
  2. Cancels pending STT response stream
  3. Moves super.dispose() to the end for proper cleanup order

This directly addresses the 588 occurrences affecting 124 users you reported.

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.

LlmChatView dispose() does not cancel pending streaming response, causing setState on disposed widget

1 participant