Fix setState() called after dispose() in UncontrolledProviderScope#4681
Fix setState() called after dispose() in UncontrolledProviderScope#4681itsatifsiddiqui wants to merge 1 commit intorrousselGit:masterfrom
Conversation
Adds a mounted check before calling setState() in the frame callback scheduled by UncontrolledProviderScope.build(). This prevents the error when ProviderScope instances are used in scrollable lists and get disposed while frame callbacks are still pending. Fixes rrousselGit#4661 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
📝 WalkthroughWalkthroughThe PR fixes a regression in UncontrolledProviderScope where setState() was called on disposed widgets during frame callbacks, causing errors when ProviderScope widgets are disposed (e.g., during PageView navigation). A mounted check is added to guard the setState invocation. The CHANGELOG documents this fix. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Related Issues
fixes #4661
Description
This PR fixes the
setState() called after dispose()error that occurs whenProviderScopeinstances are used in scrollable lists (e.g.,ListView.builder,SliverList).Root Cause
The
_UncontrolledProviderScopeState.build()method schedules a frame callback without checking if the widget is still mounted:When scrolling quickly, widgets get disposed but the scheduled callbacks still fire, causing the error.
Solution
Added a mounted check before calling
setState():Testing
Tested in a production Flutter app with:
ListView.buildercontaining widgets wrapped inProviderScopesetState() called after dispose()errorsChecklist
I have updated the
CHANGELOG.mdof the relevant packages.Changelog files must be edited under the form:
If this contains new features or behavior changes,
I have updated the documentation to match those changes.
(Not applicable - this is a bug fix with no behavior changes)
Summary by CodeRabbit
Bug Fixes